Variable declaration necessary in for loop?
问题 What is the difference between: for (var i=0; i<5; i++) {} for (i=0; i<5; i++) {} And is it necessary to include the var keyword? I understand that the var keyword affects variable scope, but I'm having trouble understanding if it's necessary to include the keyword in for loops. 回答1: In the second example, your variable is defined globally, so if you're in the browser environment, you can access it from the window object. The first one is an equivalent of: var i; for (i=0; i<5; i++) {} as all