What is function pop() doing here? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is function pop() doing here?

var arr = [45, 76, 34, 23, 85]; for(var i = 0; i < arr.length; i++) { console.log(arr.pop()); }//output:85,23,34

2nd Sep 2018, 1:31 PM
Coder21
Coder21 - avatar
2 Answers
+ 3
It removes the last element in the array. But in the same time the length of the array is decreased.
2nd Sep 2018, 1:37 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
The pop() method removes the last element from an array and returns that element. This method changes the length of the array. e.g. var arr = [45, 76, 34, 23, 85]; console.log(arr.pop()); //expected output '85', length of arr decreased from 5 to 4;
2nd Sep 2018, 3:13 PM
BraveHornet
BraveHornet - avatar