Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript hoisting explanation

Tags:

javascript

What is the difference between below snippets?

var a = 0; function b(){     a = 10;     return function a(){}; } b(); console.log(a);  // => 10 

and

var a = 0; function b(){     a = 10;     return     function a(){}; } b(); console.log(a);  // => 0 

It has something to do with JavaScript hoisting, but my understanding of the concept gives exactly the opposite output.

like image 466
SnapADragon Avatar asked Apr 17 '18 08:04

SnapADragon


People also ask

What is hoisting in JavaScript explain with example?

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.

What is hoisting function in JavaScript?

Hoisting is a JavaScript technique which moves variables and function declarations to the top of their scope before code execution begins. Within a scope no matter where functions or variables are declared, they're moved to the top of their scope.

Why does hoisting exist in JavaScript?

Hoisting is JS's default behavior of defining all the declarations at the top of the scope before code execution. One of the benefits of hoisting is that it enables us to call functions before they appear in the code. JavaScript only hoists declarations, not initializations.

What variables are hoisted in JavaScript?

Variables defined with let and const are hoisted to the top of the block, but not initialized. Meaning: The block of code is aware of the variable, but it cannot be used until it has been declared. Using a let variable before it is declared will result in a ReferenceError .


2 Answers

return function a(){}; 

Here function ... is an expression. A named function expression to be precise. The a here doesn't matter much, it just gives the anonymous function a .name, but it's still just a function expression that you're returning.

return  function a(){}; 

This here is equivalent to:

return; function a(){}; 

Here function a is a declaration, not an expression. It is hoisted, creating a local name a in the scope, shadowing the outer a. I.e. it is equivalent to:

function b(){     var a = function () {};     a = 10;     return; } 
like image 141
deceze Avatar answered Sep 16 '22 12:09

deceze


return function a() {} 

is same as

return; function a() {} 

after automatic semicolon insertion. In second case, function a is moved to the top of its scope. The code is same as

var a = 0;  function b() {   function a() {};   a = 10;   return; } b(); console.log(a); 

As a inside b() is inner function and then overriden, it is not accessible from outside of b().

Here is the demo to understand how the hoisting works.

var a = 0;    function b() {    console.log(a); // function    a = 10;    console.log(a); // 10    return      function a() {};  }  console.log(b()); // undefined  console.log(a);
like image 24
Tushar Avatar answered Sep 17 '22 12:09

Tushar