Web/JavaScript

(JavaScript) Hoisting and TDZ

haramang 2021. 10. 31. 11:48

// 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);



'Web > JavaScript' 카테고리의 다른 글

(JavaScript) Regular vs Arrow function  (0) 2021.10.31
(JavaScript) THIS Keyword  (0) 2021.10.31
(JavaScript) Hoisting  (0) 2021.10.31
(JavaScript) Scope Chain  (0) 2021.10.31
(JavaScript) Pig Game Logic  (0) 2021.10.30