Friday, May 9, 2014

javascript difference between i = 3 and var i =3

The former one will look up in its up scope to see if i is already declared. If found, the value of i will be replaced by 3. If not found, it will declare i implicitly and assign the value to it.
The latter one always declare i in the current scope and won't overwrite the value of i in its up scope.
The code below can cause endless loop. Replacing i=3 with var i=3 can solve it.

No comments:

Post a Comment