Confused about Javascript Hoisting

103 views Asked by At
function a(){
  function b(){
  }
}

In the above code of javascript, during the hoisting phase, will the function b be hoisted? Or just a will be hoisted because only function a is sitting lexically in the global context.

4

There are 4 answers

1
Quentin On BEST ANSWER

b will be hosted to the top of the scope it appears in (the scope defined by the body of function a) during the hoisting phase when that function (a) is invoked.

b will not be exported to the global scope.

0
Scott Marcus On

Declarations are hoisted to the top of their containing scope, which for function b is function a.

Function b will be hoisted to the top of function a, but that is where it already is.

And, function a (based on your code) will be hoisted to the top of the Global scope.

0
Daniel Chang On

Function a will be hoisted to top of global scope (assuming this in the global scope) and function b will be hoisted to the top of the scope created by function a.

0
Ram_T On

In hoisting process all the declarations will move up below the parent function declaration.

Ex: function fun(){
    a = 10;
    var c = b();
    function b(){}
}

will become like

function fun(){
 var a;
 var c;
 function b(){};
 a = 10;
 c = b();
}