Consider the following:
console.log(foo) // => undefined var foo = "bar"
In many other programming languages, this code would result in an error because we're trying to use
var foo console.log(foo) // => undefined foo = "bar"
Variables declared with
var are hoisted to the top of their scope (the global scope in this example). This results in the variable being initialized but not assigned.
const, which behave differently. While they are technically hoisted, an error will be thrown if they are used before they are declared.
console.log(foo) // => Cannot accesss 'foo' before initialization let foo = "bar"
That's hoisting in nutshell, although there are plenty of additional nuances to explore. This article does a fantastic job of breaking them down. I highly recommend the read if you want to explore the topic further. And, though a little more technical, the MDN reference is always a great resource.