Why the Other One with (arr[k]) is 1, 2, 3 but the Other One with Just (k) is 0, 1, 2? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why the Other One with (arr[k]) is 1, 2, 3 but the Other One with Just (k) is 0, 1, 2?

• First One var arr = [1, 2, 3]; for (let k = 0; k < arr.length; k++) { document.write(arr[k]); } • Second One var arr = [1, 2, 3]; for (let k = 0; k < arr.length; k++) { document.write(k); - Good day, pals. Just asking why the output of First One is 1, 2, 3 is not the same at the Second One?

19th Jan 2018, 1:22 AM
Vincent Matthew Benito
Vincent Matthew Benito - avatar
2 Answers
+ 3
Jonathan Pizarra, thank you, man.
19th Jan 2018, 2:03 AM
Vincent Matthew Benito
Vincent Matthew Benito - avatar
+ 6
In the first one, you are accessing and printing the values of arr using k which are: k = 0 >> arr[0] >> 1. k = 1 >> arr[1] >> 2. k = 2 >> arr[2] >> 3. so the output is 1, 2, 3. On the second one, you are only printing the values of k which are: k = 0. k = 1. k =2 . therefore 0, 1, 2.
19th Jan 2018, 1:57 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar