Created
August 13, 2018 18:05
-
-
Save rupeshtiwari/733357656b88f92a9529fbc8dc09bf90 to your computer and use it in GitHub Desktop.
What is Hoisting in Javascript
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
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