Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialZach Handler
Web Development Techdegree Student 5,332 Points.pop
When typing this array in the console :
var nums = [1, 2, 3, 4, 5, 6];
and you type nums.pop();
you get a return value of 6. But since the length of the array is now 5, shouldn't it say 5?
For example, when you do nums.push(7); it returns the number 7 (because there are now 7 items in the array).
What's the difference?
2 Answers
Steven Parker
231,172 PointsThe return value of a "pop" is not the number of items left in the array — it's the value of the item that it removes from the array. So in this example, it removes the value "6" from the end of the list.
On the other hand, "push" does return the number of items in the array.
Blake Larson
13,014 PointsPop() removes the last index of an array and returns the removed item. If it was [1,2,3,4,5,9] it would return 9 and nums is now [1,2,3,4,5].
If you do it to nums again like: let lastNum = nums.pop();
console.log(lastNum); 5 console.log(nums.length); 4