// variable hoisting console.log(counter); let counter = 1; // throws ReferenceError: Cannot access 'counter' before initialization
counter is present in the heap memory but hasn't been initialized so, it throws an error. This happens because of hoisting, the
counter variable is hoisted here.
// function hoisting const x = 20, y = 10; let result = add(x,y); // ❌ Uncaught ReferenceError: add is not defined console.log(result); let add = (x, y) => x + y;
add function is hoisted and initialized with
undefined in heap memory in the creation phase of the global execution context. Thus, throwing an error.