Both the function and variable declarations are hoisted, but functions declarations are hoisted before variables.
So your code:
var z;
function b(){}
function a(){}
when compiled by JS engine will become:
function b(){}
function a(){}
var z;
Now the question is will below code work without errors?
var z;
function b(){a();}
function a(){}
In the above code function a
is called in function b
before it is declared. JS engine works in two phases first is compiler phase in which all hoisting will occur and second is execution phase in which execution will occur.
So when you call to function a
(which will eventually occur in execution phase) will occur after the declaration of function a
in compiler phase.
For example.
When you write:
console.log(a); // undefined cause var a; is declared first
var a = 10;
above code will be interpreted by JS engine as,
var a ;
console.log(a); // undefined;
a = 10;
For more information about Hoisting.