Last active
August 29, 2015 14:02
-
-
Save Gwash3189/dea43a6dcf67863a2f8d to your computer and use it in GitHub Desktop.
JS Hoisting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hoist() { | |
for(var i = 0; i < 10 i++){ | |
var test = "Test"; | |
} | |
console.log(test); | |
} | |
function hoist2() { | |
var test; | |
for(var i = 0; i < 10 i++){ | |
test = "Test"; | |
} | |
console.log(test); | |
} | |
var myvar = "im a global"; | |
function scopeLevels(){ | |
alert(myvar); // expected "global variable", result: undefined | |
var myvar = "i am not a global"; | |
alert(myvar); // local variable | |
} | |
var myvar = "im a global"; | |
function scopeLevels2(){ | |
var myvar; | |
alert(myvar); // expected "global variable", result: undefined | |
myvar = "i am not a global"; | |
alert(myvar); // local variable | |
} | |
function functionalDecs() { | |
//do some stuff | |
functionalexpressionExample() | |
//morestuff | |
myvar = function(){}; | |
myvar(); | |
function functionalExpressionExample(){}; | |
} | |
function bestPracticesBad() { | |
var myvar = 5 | |
if(myvar === 5){ | |
var somthing = somthingElse; | |
var thisAndThat = thatAndThis; | |
//do something with myVar | |
} | |
somthing = "this code makes me sad" | |
} | |
function bestPractiesGood() { | |
var myVar = 5; | |
var somthing = somthingElse; | |
var thisAndThat = thatAndThis; | |
if(myVar === 5){ | |
//somthing | |
} | |
somthing = "this code makes me happy" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment