let and const are the block scoped
A block is a chunk of code bounded by {}. A block lives in curly braces. Anything within curly braces is a block.
- Ex1:
let greeter = "hey hi";
if (1) {
let greeter = "hello";
}
console.log(greeter);
let greeter = "hey hi";
if (1) {
let hello = "hello";
}
console.log(greeter);
console.log(hello);
let can be updated but not re-declared. const can't be updated or re-declared
- Ex2:
OK
let greeting = "say Hi";
greeting = "say Hello instead";
NOT OK
let greeting = "say Hi";
let greeting = "say Hello instead"; // error: Identifier 'greeting' has already been declared