I am unable to understand initialization

let arr=[ ]
for(let i=0;i<5;i++)

Now If i do arr.length I get 0

Instead of console.log(arr[i]), if I put arr[i]=undefined in the loop
arr.length comes out to be 5

Why is this so? Isn’t javascript initialising the values with undefined in the first case? Because it is console.logging undefined in that case.

When you declares arr = [ ] you are saying that exist and empty array. Remember that and array consist in several values in a list, then when in the loop you say arr[i] = undefined, you are saying that that position exist in de array, but is empty, think it as the diference to make a empty list:


And make a list with points to anote those things:

Things to do

In the second case you are creating those “positions” even if you let empty, the marker exists

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.