Skip to content

Instantly share code, notes, and snippets.

@rupeshtiwari
Created August 13, 2018 18:05
Show Gist options
  • Save rupeshtiwari/733357656b88f92a9529fbc8dc09bf90 to your computer and use it in GitHub Desktop.
Save rupeshtiwari/733357656b88f92a9529fbc8dc09bf90 to your computer and use it in GitHub Desktop.
What is Hoisting in Javascript
var x = 5;
function print() {
if(x==5) {
var x = 6;
alert(x);
} else {
x = 7;
alert(x);
}
}
/**
Here line number 4 x is declared and initialized.
Therefore, x will be moved at the top of the function
it will move to line number 3 and initizlie x with undefinied.
var x = 5;
function() {
var x;
if(x==5) {
x = 6;
alert(x);
} else {
x = 7;
alert(x);
}
}
therefore after calling this function it will alert 7 only.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment