Javascript hoisting:
In JavaScript, Hoisting is the default behavior of moving all the declarations at the top of the scope before code execution. Basically, it gives us an advantage that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or localWe can call as all the declaration variable going to top while executing the application.
Above image assigning x=1 and it is not declared, after creating the alert then declaring the variable.
Here it is not throwing any error while executing javascript declarations are going to top.
Here var x: is going to top then assigning x=1:
Code snippet:
// program to display value
var a = 6;
function greet() {
b = 'hi';
console.log(b); // hi
var b;
}
greet(); // hi
console.log(b);
Output :
Hi
Uncaught ReferenceError: b is not defined
Comments
Post a Comment