Arif Bilgin
Arif Bilgin

Reputation: 25

javascript variable scope for uninitialized local variables

I am having a problem with understanding the difference between

for (i = 0; i < 4; i++) {
    var g;
    if (g === undefined) {
        g = 0;
    } else {
        g = g + i;
        alert(g);
    }
}

and this

for (i = 0; i < 4; i++) {
    var g=0;
    if (g === undefined) {
        g = 0;
    } else {
        g = g + i;
        alert(g);
    }
}

for the first loop output is 0 ,1 ,3 ,6 which indicates that value of g is not reset throughout the iteration , however if g is initialized with zero output becomes 0, 1, 2, 3 .That tells me that each time var g=0 is hit g is set to zero.

Does that mean that if local variables are not initialized they behave like globals?

Upvotes: 1

Views: 407

Answers (3)

jfriend00
jfriend00

Reputation: 707318

Your loop outputs 0,1,2,3 so you're working under some sort of false assumption about what your code does. See http://jsfiddle.net/jfriend00/9uo5g5fo/ for a running demo to see the actual output.

Local variables that are declared but not initialized are still local variables. They only become global variables if you are not running in strict mode and if you do not declare them before using them (which is a bad practice to follow). I'm not sure why you're asking about this because it doesn't effect your current code.

The current version of Javascript (ES5) has only function scope for variables so all variable declarations within a function are hoisted to the top of the function or scope so your code:

for (i = 0; i < 4; i++) {
    var g=0;
    if (g === undefined) {
        g = 0;
    } else {
        g = g + i;
        alert(g);
    }
}

is equivalent to this:

var g;
for (i = 0; i < 4; i++) {
    g=0;
    if (g === undefined) {
        g = 0;
    } else {
        g = g + i;
        alert(g);
    }
}

The reason your code outputs 0,1,2,3 is because g will be set to 0 at the start of every iteration of your for loop and thus the first if will never be satisfied so the else will always execute which will cause g = g + i to be executed. But, since g is always 0, that will be like setting g = i, so your alert(g) will just be showing the value of i.

Your code is essentially just this:

for (i = 0; i < 4; i++) {
    alert(i);
}

FYI, the next version of Javascript (ES6) offers the let keyword so you can declare variables with block scope, but that version is only partially implemented in some of the latest versions of browsers so isn't generally available for widespread browser use yet. It can be used in some non-browser uses of Javascript.

Upvotes: 0

Cassie
Cassie

Reputation: 96

This has to do with variable hoisting and scope. Since for loops do not alter scope in javascript, the first code block is interpreted by Javascript as

var g;
for (i=0;i < 4;i++)
{
    g=0;
    if(g===undefined)
        g=0;
    else
        g=g+i;
    alert(g);

}

g is therefore reset on each iteration of the loop, outputting "0 1 2 3". The second code becomes

var g;
for (i=0;i < 4;i++)
{
    if(g===undefined)
        g=0;
    else
        g=g+i;
    alert(g);
}

and is not reset on each iteration of the loop, thus outputting "0 1 3 6".

Upvotes: 4

levi
levi

Reputation: 22697

nope, local vars are local vars,the second loop output is 0, 1, 2, 3 because you declared g but without any value, so g===undefined is always true and g=0 always get hit.

In your first loop output is 0 ,1 ,3 ,6, because g===undefined is always false, because you declare g and assigned it 0, so g=0 is never touched.

Upvotes: 0

Related Questions