Brad Mash
Brad Mash

Reputation: 69

JavaScript Global Var Hoisting Issue

Here is my basic issue...

<script>
    var window.my_Global = false;
</script>

<script>
    if(my_Global){...} //my_Global here is undefined, not false??
</script>

I know there must be a simple solution but I'm pretty stuck...

Upvotes: 1

Views: 56

Answers (1)

Cerbrus
Cerbrus

Reputation: 72917

Remove var:

window.my_Global = false;

You don't need the var statement to set properties of an object. In fact, that's invalid syntax:

<script>
    window.my_Global = true;
</script>

<script>
    console.log(my_Global);
    if(my_Global){
        alert("Hello world!");
    }
</script>

Upvotes: 4

Related Questions