8. Variables: The Modern let Keyword
Introduced in ECMAScript 2015 (ES6), let is the preferred way to declare variables that might need to be reassigned later.
Block Scope
The most important difference between var and let is block scope. A 'block' is any code contained within curly braces {}.
javascript let userAge = 25;
if (userAge > 18) { let accessStatus = 'Granted'; console.log(accessStatus); // Output: Granted }
// console.log(accessStatus); // ERROR: ReferenceError: accessStatus is not defined
// The variable is confined to the if-block.
Redeclaration is Forbidden
Unlike var, you cannot declare the same let variable twice in the same scope.
javascript let city = 'New York'; // let city = 'London'; // ERROR: SyntaxError: Identifier 'city' has already been declared
// But reassignment is fine: city = 'London'; // This is okay.
Rule of Thumb: Use let when you know the value of the variable will change.