Skip to main content

javascript hoisting

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 local

We 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

Popular posts from this blog

దోసకాయ పూర్ణం | cucumber curry

How to merge Objects in Javascript

 let person = {     firstName: 'Rocky',     lastName: 'g',     age: 23 }; let job = {     jobTitle: 'Angular Developer',     location: 'UK' }; let employee = {     ...person,     ...job }; console.log(employee);

క్యారెట్ ఊరగాయ | carrot pickle