Let’s see an example, we have a variable i with var keyword and we assigned 10 to it.
var i = 10; console.log(i); // Output: 10
It returns 10, which is very simple. In variable i we stored 10 and console.log(i) simply prints in the browser console.
Now look at the example below, in which we have moved variable i to down. In this case we will be getting different results.
console.log(i); // Output: undefined var i = 10;
let i = 10; console.log(i); // Output: 10
We have output 10 in the browser console. Now let’s change the order,
console.log(i); // Output: Uncaught ReferenceError: Cannot access 'i' before initialization let i = 10;
This time we got the below error message in the browser console.
Uncaught ReferenceError: Cannot access 'i' before initialization
It says it can’t be accessed before initialization which means the hoist mechanism does not initialize any value to the i variable because of the let keyword. That’s why we say the let keyword is a non hoisted variable.