Question about array.push

Question about array.push
0.0 0

#1

This seems like a pretty basic question so I am surprised that I am coming across it now.

I am doing some practice exercises on javascript arrays and these were the steps involved:

var myArray = ['Manchester', 'London', 'Liverpool', 'Birmingham', 'Leeds', 'Carlisle'];
var myNewArray = myArray.push('Lusaka');
myArray 
//result: (10) ["Manchester", "London", "Liverpool", "Birmingham", "Leeds", "Carlisle", "Bradford", "Brighton", "Bristol", "Lusaka"]
myNewArray 
//result: 10

It’s that last bit of code that I am confused about. Why is myNewArray equal to length…? And not equal to an array (i.e. the same array as myArray, in this case?)

I am willing to accept this if that’s just the way arrays work but was anyone else confused when they first cam across this…?

Thanks!


#2

Scroll down to the syntax section. Look at what the parameters and return values are. I can’t tell you how many times I have googled “mdn array FUNCTIONNAME”. I live there now.


#3

From MDN:

The push() method adds one or more elements to the end of an array and returns the new length of the array.

If myArray.length = 7, and you then add “Lusaka” using myArray.push(“Lusaka”), js returns the number 8, the new length of the array.

In your example, you are setting myNewArray equal to the number returned by the push() method.