So, let’s not wait further and dive into concept of hoisting.
var are lifted to the top of their functional/local scope (if declared inside a function) or to the top of their global scope (if declared outside of a function) regardless of where the actual declaration has been made.
Basically, it gives us an advantage that no matter where variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local. It allows us to use variables before it is declared in our code.
The following example declares variable
studentName and assign value
John to it:
console.log(studentName); // usage var studentName = 'John'; //declaration & assignment
No Matter where function and variable are declared, it moved up top on their own scope. Doing this, it allows us to call functions before even writing them in our code.
How interpreter sees the above code:
let us take another example,
ES6: Let Keyword
Like before, for the var keyword, we expect the output to be undefined. But this time we got a reference error. That Means let and const variables not hoisted? The answer is Variables declared with let are still hoisted, but not initialized, inside their nearest enclosing block. If we try to access it before initializing will throw ReferenceError due being into Temporal Dead Zone.
Hoisting moves variables and functions to the beginning of the scope. But this term is no longer valid for
class: and here’s why.