Reputation: 147
Why does this code print out 4
rather than ['B', 'e', 'e', 'a']
?
var word = 'Bee';
var letters = word.split('').push('a');
console.log(letters);
// -> 4
Upvotes: 1
Views: 64
Reputation: 315
Run
var word = 'Bee';
var letters = word.split('');
letters.push('a');
console.log(letters);
Upvotes: -2
Reputation:
As stated here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
The push() method adds one or more elements to the end of an array and returns the new length of the array.
So that is why letters
assigned as a Number
rather than the array.
Upvotes: 3
Reputation: 331
4 is the length of the new array, since you're pushing an element onto a list of characters.
Upvotes: 0