Why does it display "undefined"? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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