Hoisting in JavaScript
Moving the declaration of variable and function at the top is called Hoisting.
First function declaration is hoisted and then variable declaration is hoisted.Example 1
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
console.log(a); // undefined | |
var a = 10; | |
console.log(a); // 10 |
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 a; | |
console.log(a); // undefined | |
a = 10; | |
console.log(a); // 10 |
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
console.log(a); // undefined | |
var a = 10; | |
console.log(a); // 10 | |
greet(); // "Hello" | |
function greet() { | |
console.log("Hello"); | |
} |
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 greet() { | |
console.log("Hello"); | |
} | |
var a; | |
console.log(a); // undefined | |
a = 10; | |
console.log(a); // 10 | |
greet(); // "Hello" |
No comments:
Post a Comment