6

I am running into a strange scope issue with Javascript (see JSFiddle):

var someGlobal = 3;

function someF() {
    // undefined issue
    alert(someGlobal);
    var someGlobal = 5;
    // Displays 5
    alert(someGlobal);
}

function someF2() {
    // Displays 3, why?
    alert(someGlobal);
}

someF();
someF2();

Why doesn't Javascript throws an undefined issue in someF2()? How come someF2() can access the someGlobal, and someF() not? How can I make sure a global variable is accessible in a function?

Remark:

In both cases, the functions start by calling alert(someglobal), why does one function throw an undefined issue and the other not?

4 Answers 4

7

someF creates a new (locally scoped) variable called someGlobal (which masks the global someGlobal) and assigns a value to it. It doesn't touch the global someGlobal (although cannot access it because there is another variable with the same name in scope).

var statements are hoisted, so someGlobal is masked for all of someF (not just after the var statement). The value of the local someGlobal is undefined until a value is assigned to it.

someF2 access the original (untouched) global someGlobal.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 for best answer but describe more that global variable gets overwrite by local variable in function :D
It doesn't get overwritten, it gets masked.
well can you give me a little explanation about both! i haven't done research on it! For only that function it gets overwritten or not?
Are you saying that because someF() declares a local variable called someGlobal after the first alert, this impacts this first alert() by having it throw the undefined issue?
4

Since you are declaring a local variable with the same name. So it assigns the value to the local variable. Just remove the var from var someGlobal in someF() and it should be fine.

var someGlobal = 3;

function someF() {
    // undefined issue
    alert(someGlobal);
    someGlobal = 5; // <-- orignially var someGlobal = 5
    // Displays 5
    alert(someGlobal);
}

function someF2() {
    // Should display 5 now
    alert(someGlobal);
}

someF();
someF2();

Comments

1

someF2 displays 3 because it still is 3.

In someF() you create a new variable that happens to have the same name as someGlobal. That does not do anything to the original someGlobal, it just creates a new variable locally to function someF that goes away when that function ends.

So you have local variables (e.g. created inside someF with var) and global ones.

2 Comments

Ok, but why do I get an undefined issue in one case and not the other? That's my question.
I think Quentin has now explained that.
-1

Here is an example of how to use both the local and global variable inside someF by using this.

var someGlobal = 3;

function someF() {

    // Displays 3
    alert(someGlobal);
    this.someGlobal = 5;
    someGlobal = 5;
    // Displays 5
    alert(this.someGlobal);
}

function someF2() {
    // Displays 5
    alert(someGlobal);
}

someF();
someF2();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.