(JavaScript) Hoisting and TDZ

(JavaScript) Hoisting and TDZ

// HOISTING and TDZ--------------------------------------------------------------------------------------------

// Variables

console.log(me); // Okay

// console.log(job); // Error - let

// console.log(year); // Error - const

var me = 'Hongsik';

let job = 'Developer';

const year = 1991;

// Functions

console.log(addDecl(2, 3)); // Okay

// console.log(addExpr(2, 3)); // Error - Var is declared as undifined - undifned(2,3) - this is what you did

// console.log(addArrow(2, 3)); // Error - Var is declared as undifined - undifned(2,3) - this is what you did

function addDecl(a, b) {

return a + b;

}

const addExpr = function (a, b) {

return a + b;

};

var arrArrow = (a, b) => a + b;

// Example

if (!numProducts) {

// Even thouh numProducts is set to 10

deleteShoppingCart(); // With hoisting, it is declared with value 'undefiend'

}

var numProducts = 10;

function deleteShoppingCart() {

console.log('All products deleted!');

}

var x = 1;

let y = 2;

const z = 3;

console.log(x === window.x);

from http://haramang.tistory.com/101 by ccl(A) rewrite - 2021-10-31 12:01:51