Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable scope in Javascript for loop

Tags:

What's the difference between:

function bar() {   for (x=0; x< 100; x++) {} } 

And

function bar() {   var x;   for (x=0; x< 100; x++) {} } 

If x wasn't declared outside of that function, and so it wasn't a global variable? I always wonder this because I generally don't declare throwaway variables which are used only in a loop, but I wonder if this might break comparability in a browser or such.

like image 568
Ali Avatar asked Apr 26 '09 09:04

Ali


People also ask

What is the scope of a variable in a for loop?

The variable is within the scope of the loop. I.e. you need to be within the loop to access it. It's the same as if you declared a variable within a function, only things in the function have access to it.

What is loop scope in JavaScript?

Typically used to initialize a counter variable. This expression may optionally declare new variables with the var keyword. These variables are not local to the loop, i.e. they are in the same scope the for loop is in. The result of this expression is discarded. Follow this answer to receive notifications.

Does a for loop have its own scope?

However, the above while loop would still give an output of 3 3 3 , so the previously shown for loop can't be equivalent to it. The explanation is that a for loop has an own scope for every iteration.

Why do we use let in for loop?

According to MDN using let in the for loop like that should bind the variable in the scope of the loop's body. Things work as I'd expect them when I use a temporary variable inside the block.


2 Answers

The first example will either add or modify the global variable x, which is generally to be avoided if not the desired outcome.

While your second example works as desired (no side effects) an alternative that looks better in my opinion would be

function bar() {   for (var x=0; x< 100; x++) {} } 
like image 109
cobbal Avatar answered Sep 20 '22 08:09

cobbal


A variable is created at the time you declare/use it. If you omit the var keyword than the variable is created automatically in the global scope. So you produce a side effect. This is generally to be avoided.

Assume you use global variables and then you chose the name of variable that some other piece of the software has already taken. This will lead to a situation where to pieces of code overwrite their values. This produces errors and is most of the time hard to debug. In your example you might overwriting a global variable x that another software is using.

Using var is also faster. If you access a global variable it has to scan all scopes up to the global one for the variable name. By using var it is bound to your local scope.

It is good practice to use always use var. Or better: it is always good to select the most narrowed scope for your variables. Now you have global and var. A var declaration is visible in the whole function no matter where you declare it. In javascript 1.7 there is new keyword introduced: let. Let narrows the scope even more. If you declare your for loop with

for(let x = 0; x < 100; i++) {} 

than x is visible only inside the {} block.

like image 27
Norbert Hartl Avatar answered Sep 21 '22 08:09

Norbert Hartl