Let’s learn JavaScript Hoisting

When was the last time, you encountered confusing questions on “JavaScript Hoisting” in your interview?

Not very long back if you are looking for change 🙂

Today’s topic is very simple, but most misunderstood concept in our coder’s fraternity.

Lets try to learn sharpen our concepts and be ready to face the interview panel with confidence.

Javascript Hoisting
Hoisting



What is JavaScript Hoisting?


JavaScript Hoisting(Raise Up), in simple words refer to moving declarations to the top.(Like our flag hoisting ceremony in school times ).

Technically it is the default behavior of JavaScript, to shift all the declarations to the top of the function or global scope .


Principle:-


In other words, declaring a variable anywhere in the code is equivalent to declaring it at the top which allows a variable to be used before it is declared. 

However, only the declaration will be hoisted. If the variable is initialized, the current value, at the top of the scope, will initially be set to undefined.
Meaning:-JavaScript only hoists declarations, not initialization.


Example:-

Variable Hoisting

function bar(){
function foo(){}
var a=7;
}

Will be interpreted as:

function bar(){
var a;//initialized to undefined
function foo(){}
a=7; // assignments are not affected by Hoisting
}


Function Hoisting
Note:- Variable name = function name = foo

var foo =3;
function bar(){
foo= 7;
return;
function foo(){}
}
bar();
alert(foo);
will be interpreted as:

var foo =3;// defines foo in global scope
function bar(){
function foo(){} // defines foo in local scope and can be rewritten as “var foo = function(){}”
foo= 7; // overwrites local variable foo
return;
}
bar();
alert(foo);




In JavaScript, functions are the first class objects like strings and numbers which means they are assigned to variables that can be passed to other functions as well.

Explanation:-

  • Function foo() is hoisted first and it will act like var foo = function(){};,as a result a local variable foo is created
  • Local variable will always get precedence over global variable if there are two variables with same name (one in local, another in global).
  • In above example, foo=7; means setting the value in the local scope. So the result of alert will be 3
  • JavaScript does not have block statement scope; rather, it will be local to the code that the block resides within.
  • Declaring variables in a function means that variables declared in a function are accessible within that function, even before they are assigned a value.


How Name Resolution Order works:-

  • Both local and global scope, have the special name this, defined in them , which points the current object.
  • Function scope also has the named arguments , defined in them which are passed to them.

Confused!! Lets comprehend with example,

For example, JavaScript will find the name in the following order when accessing a variable named bar inside the scope of a function.
1.    Use var bar; statement if it is present in current/function scope.
2.    Use function parameter if it is having the name as “bar”.
3.    Use function itself, if its name is “bar”.
4.    Look in the outer scope and repeat the entire process from point 1.

Key Learning’s:

Always declare variables at the top of the scope (function/global) 


Leave a Reply

Your email address will not be published. Required fields are marked *