Behind the Scenes, you will see that JavaScript always takes all the declarations first. That’s basically the hosting. Since this is the default behavior in JavaScript. In this article, I will discuss only the hosting specific.
Since hosting is the default behavior, all functions in JavaScript are hosted in the Creation phase. That’s why we can actually call a function before declaring it.
customFunc();
function customFunc(){
let a = 77;
let b = 4;
let sum = a + b;
console.log('Sum : ' + sum)
}

The reason for working is for JavaScript default behavior in the Creation phase. Here are some points for convenience:
- The arguments list of the arguments that are passed to the function is created
- The code scans all the functions and each function is stored in a variable object that usually points to the function.
- Then it looks for the declarations of the code variables. And
undefinedsets the property for each declared variable .
Then the code goes to the execution phase and the line-by-line code is executed according to the execution context.
And that is why since function declaration is earlier in the creation phase and the function call is executed in execution phase, we can use it even before declaring function.
But function expressions will not work this way. Because in function expressions we store our functions in a variable. And that variable undefinedis set as the creation phase . The original function is executed later, so calling the function before declare in the function expression will show the error as it is not a function. Because it’s not really a function right now, just a variable.
miew();
var miewVar = function(){
console.log('Miew is calling')
}

Similarly, we can use a variable before declaring it. If you use a variable before declaring it undefined because of the Creation phase, it will not show any error, but it will show that variable undefined as it is set in Creation Phase .
console.log('Value of Miew: ' + miew);
var miew = 10

But if you have never declared that variable later, you will want to use it:
console.log('Value of C: ' + miewC);

So hopefully this time, there is a clear idea about hosting. However, for good practice it is not advisable to use a function before declaring it. But there’s definitely a lot of work to be done to understand how JavaScript works within the scene. In some situations, it may be helpful to have such a technique.