This allows us to use a function above its declarations as all the variables are moved to the top of their scope regardless of whether their scope is global or local. It’s important to keep in mind that only the actual declarations are hoisted and that assignments are left where they are.
Here is a small example explains the same.
console.log(num); var num; num = 6; // this will return undefined as Hoisting only hoist declarations.
However, this will work as Hoisting moves the declarations to the top.
num = 6; console.log(num); var num; // return 6
We can understand this with the help of this given code snippets.
var declaredLater; // Outputs: undefined console.log(declaredLater); declaredLater = "Now it's defined!"; // Outputs: "Now it's defined!" console.log(declaredLater);