Does variable declared with let or const hoisted?

JavaScript Hoisting is a process where the compiler allocates memory for variable and function declarations before the code execution.

If we declare a variable using var then, it is initialized with a default value of undefined.

Variables declared with let and const are defined but not initialized as part of hoisting.

let, const hoisting:

In JavaScript, all declarations that mean var, let, const, function, function*, class are hoisted.

Let us consider the following example:

console.log(carBrand);    // undefined
console.log(techCompany); // Uncaught ReferenceError: Cannot access 'techCompany' before initialization

var carBrand = ['BMW', 'Audi'];
let techCompany = ['Google', 'Apple'];

Notice that let techCompany is a declaration but it does not initialises the variable. Until the variable initialization is executed, any code that accesses these variables will throw an exception.

This situation is called the temporal dead zone. let/const variables cannot be read or written until they have been fully initialized and accessing the variable before the initialization results in a ReferenceError.

Let us consider another example:

let getName = () => name;
let name = 'Joe Smith';
console.log(getName()); // Joe Smith

From the above example if we try to find out how the name was printed on the console, then the hoisting for const/let will be clearer.