var arr = [1,2,3,4,5]
for (var i=0; i<arr.length; i++){
console.log(arr[i]);
}
I ran this code on google chrome console and the output was like this:
1
2
3
4
5
In this code I can see that all the elements are been printed but the very first element of the array which is 1 should not print because the value of [i] has already been incremented to 1 and so 2 should be printed instead of 1.
Can someone tell me why is this happening ?
I think you are getting it wrong here. In for loop, we have
for(initialization, condition, change) {
// body
}
So, it workes in this sequence
initialization -> condition -> body -> change
PS: It will go to body if condition evaluates to true