Skip to content

Instantly share code, notes, and snippets.

@Gwash3189
Last active August 29, 2015 14:02
Show Gist options
  • Save Gwash3189/dea43a6dcf67863a2f8d to your computer and use it in GitHub Desktop.
Save Gwash3189/dea43a6dcf67863a2f8d to your computer and use it in GitHub Desktop.
JS Hoisting
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