Scope
If you’re coming from another language, scope in JavaScript will probably be pretty intuitive… with some exceptions.
Some programming knowledge is required
Let
let foo = 1;
{
let foo = 2;
}
console.log(foo); // 1
let foo = 1;
{
foo = 2;
}
console.log(foo); // 2
Const
const foo = 1;
{
const foo = 2;
}
console.log(foo); // 1
const foo = 1;
{
foo = 2; // error
}
Var
var foo = 1;
{
var foo = 2;
}
console.log(foo); // 2
var foo = 1;
{
foo = 2;
}
console.log(foo); // 2