Variable & Function hoisting in javascript

Hoisting is JavaScript's default behavior of moving declarations to the top of the current scope.

Hoisting can be applied to variable declarations and to function declarations. As a result of this, JavaScript functions & variables can be called before they are declared.

For example : - 

1. variable Hoisting : 
x = 8; 
console.log(x); // prints  8

var x;

2. Function Hositing : - 
displayName();

function displayName () {
     console.log("I am rroxy");
}
* Function Hoisting applies to only functions not functions expressions, e.g : -
displayName(); // undefined

var displayName = function () {
console.log("I am rroxy");
}

Comments

Popular posts from this blog

What is MQTT protocol & its use

What the hack is Call, bind & apply

What is Provider in Angular Js?