console.log(myArray.pop()); VS console.log(myArray.push());

Tell us what’s happening:
As you see here: console.log(myArray.push()); gives the new length of the new array but console.log(myArray.pop()); still gives the rest of the array itself
Why is it so different as a result ?

Your code so far


// Setup
var myArray = [["John", 23], ["cat", 2]];

// Only change code below this line
var removedFromMyArray;
removedFromMyArray = myArray.pop()
console.log(myArray.pop());
console.log(myArray.push(1, 2));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.74.

Challenge: Manipulate Arrays With pop()

Link to the challenge:

pop returns the removed element

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop

I imagine it is returned because otherwise it is lost

push instead change the array by adding items, and it returns the new length - why? This I have no idea why it was choosen

2 Likes

pop() is getting an item off the end of an array.
push() is for adding an item to an array.

They are used for opposite purposes, so they have different return values.

2 Likes

Thank you both :smiley: