var, let, and const in scope?

  • var → Function scoped.
  • let & const → Block scoped {}.

Example:

if (true) {
  var x = 1;   // accessible outside
  let y = 2;   // block scope
  const z = 3; // block scope
}
console.log(x); // 1
// console.log(y); // error

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *