JavaScript Variable Hoisting Example

by Lucas Krause

JavaScript

(function(){
    if(false){
        var a=42;
    }
    
    console.log(a); //no error (undefined)
})();

//is equal to

(function(){
    var b;
    
    if(false){
        b=42;
    }
    
    console.log(b); //no error (undefined)
})();

(function(){
    console.log(c); //error
})();

/**
 * Reference: http://teamtreehouse.com/library/javascript-foundations/variables/hoisting
 */