Hoisting variables in JavaScript

This is how the interpreter sees your code,

do_something() {
 var foo;
 console.log(foo); // undefined
 foo = 2;
}

do_something();

So it is printing undefined. This is a basic of variable hoisting. Your declarations will be moved to the top, and your assignation will remain in the same place. And the case is different when you use let over var.

Leave a Comment