Augustus Brennan
Augustus Brennan

Reputation: 147

Why does this return a number?

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

Answers (3)

BeckiD
BeckiD

Reputation: 315

Run

var word = 'Bee';
var letters = word.split('');
letters.push('a');
console.log(letters);

Upvotes: -2

user6793977
user6793977

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

Chris Zimmerman
Chris Zimmerman

Reputation: 331

4 is the length of the new array, since you're pushing an element onto a list of characters.

Upvotes: 0

Related Questions