+ 1

Could you explain this code for me

let arr = [1, 2, 3]; for (let k = 0; k < arr.length; k++) { console.log(arr[k]); } //Output is 1 2 3 //why is the output like this 123

13th Aug 2019, 4:56 AM
Safaa Alnabhan
Safaa Alnabhan - avatar
1 Odpowiedź
+ 2
The variable k will be 0, then 1, then 2. Computers start counting at 0, so to access the first element of the array we have to use the index 0 (arr[0] is 1). So then we print the second element (index 1) and the third (index 2).
13th Aug 2019, 5:30 AM
Paul Grasser
Paul Grasser - avatar