+ 2

Why does it display "undefined"?

var man = [8, 6, 7]; man.pop(); console.log(man[2]); Explain, please 🙏

21st Jul 2020, 11:15 AM
Кун
Кун - avatar
7 Respostas
+ 4
Pop method used to eliminate the last element of the array and according to your process you want to put in console the element you have just eliminated so it will show undefined as you have already cleaned up that element adress in memory Var man[8,6,7] Man.pop =[8,6] Console.log[2] =undefined
21st Jul 2020, 11:25 AM
ebrahem hesham
ebrahem hesham - avatar
+ 6
Because you removed the last item in the array ( on index [2] ) with the pop() method. That's why it's empty/undefined now.
21st Jul 2020, 11:27 AM
Trinity ⚜
Trinity ⚜ - avatar
+ 6
Sorry..the other answers weren't there before I started writing. :)
21st Jul 2020, 11:32 AM
Trinity ⚜
Trinity ⚜ - avatar
+ 3
Hi, pop method deletes last element of your array and returns it. So your array doesn't have this element more.
21st Jul 2020, 11:26 AM
Maksim Rukavishnikov
Maksim Rukavishnikov - avatar
+ 2
Remember also that the array started from zero index not one
21st Jul 2020, 11:26 AM
ebrahem hesham
ebrahem hesham - avatar
+ 2
Thank you very much🙏 now I understand😅👌
21st Jul 2020, 11:29 AM
Кун
Кун - avatar
+ 2
Anyway, thank you very much for not showing indifference) 🤗
21st Jul 2020, 11:38 AM
Кун
Кун - avatar