Situation 1: The expected result here would be to see the text John written in the console, but surprisingly, we see undefined gets logged instead. Wonder why?

Well, here we can see the classic JavaScript in action. This behavior is called hoisting. Under the hood, the language splits the variable declaration and value assignment into two pieces. The variables are moved to the top, declared with the value set to undefined (hoisted), regardlessly of where they were initially declared by a developer. It looks something like this:

var person; console.log(person); person = 'John';

Situation 2: Here, the result will be a reference error.

Uncaught ReferenceError: Cannot access 'person' before initialization

The error text speaks for itself. Because we used the keyword let , our variable is hoisted but remained uninitialized, and the error gets thrown informing us that we are trying to access an uninitialized variable. The let keyword was introduced in ES6 enabling us to use block-scoped variables thus helping us prevent unintended behavior.

Situation 3: Here, we have the same error as in the Situation 2.

The difference is that we've used the keyword const , thus preventing re-assigning our variable after initialization. This keyword was also introduced in ES6.

Situation 4: In this case, we can see how the keyword const is useful and how it can save us from unintentionally re-assigning our variable. In our example, first, we will see Vanessa written in the console, and then a type error.

Uncaught TypeError: Assignment to constant variable

The usefulness of const variables grows exponentially with our codebase.

Situation 5: If a variable has already been defined inside a certain scope with the keyword var , trying to declare it again with the keyword let inside the same scope throws an error.

So, in our example, nothing will be logged and we will see a syntax error.

Uncaught SyntaxError: Identifier 'person' has already been declared

Situation 6: We have a function-scoped variable first and block-scoped variable second. In this case, it doesn't matter if they have the same name/identifier.

In the console, we should see Mike and John getting logged, in that order. Why?