0
What is the output of this code and why?
arr=[1, 2, 3, 4]; arr[5]= 5; alert(arr.length);
3 Answers
+ 1
6
Arrays use 0 indexing - first one is 0. So every place in array counts as +1. Here's the full array:
arr = [1, 2, 3, 4, 0, 5]
Index 4 is none^
+ 1
I think he's just using the 0 as a placeholder. The array actually would look like,
[1,2,3,4,,5]
if you output it as a whole.
The actual value held at index 4, I.E. arr[4]
is undefined.
0
From where the zero come from đ€