In JavaScript, the keywords let and const are used to declare variables. They were introduced in the ES6 (ECMAScript 2015) version of JavaScript as alternatives to the var keyword.
Here's an explanation of let and const and their differences:
let
- Variables declared with let have block scope. They are only accessible within the block they are defined in (typically within curly braces {}).
- You can declare a variable with let and then reassign its value later in the same scope.
Example 1
let x = 5;
if (true) {
let x = 10;
console.log(x); // Output: 10
}
console.log(x); // Output: 5
Example 2
function example() {
let x = 5; // Variable x is declared with let inside a function
if (true) {
let x = 10; // A new block-level variable x is declared
console.log(x); // Output: 10
}
console.log(x); // Output: 5 (the original x is still accessible)
}
example();
In this example, let is used to declare the variable x within both the function and the if statement block. The block-level x inside the if statement shadows the function-level x, and each block has its own separate variable.
Example 3
for (let i = 0; i < 3; i++) {
console.log(i); // Output: 0, 1, 2
}
console.log(i); // ReferenceError: i is not defined
Here, let is used to declare the loop variable i. Since let has block-level scope, the variable i is only accessible within the for loop block. Hence, the reference error when trying to access it outside the loop.
Example 4
console.log(x); // Output: ReferenceError: x is not defined
let x = 5;
Unlike var, variables declared with let are not hoisted to the top of their scope. Trying to access a let variable before it is declared will result in a reference error.
Example 5
let x = 5;
let x = 10; // SyntaxError: Identifier 'x' has already been declared
With let, you cannot redeclare a variable within the same scope. Attempting to do so will result in a syntax error.
const
- Variables declared with const also have block scope, like let.
- The const keyword is used to declare constants, which are variables that cannot be reassigned once they are defined.
- A constant must be assigned a value at the time of declaration and cannot be left uninitialized.
const PI = 3.14;
console.log(PI); // Output: 3.14
PI = 3.1415; // Error: Assignment to constant variable
In this example, const is used to declare a constant variable PI with a value of 3.14. Once a constant is defined, its value cannot be reassigned, hence the error in the second line.
- let allows you to declare variables that can be reassigned, while const declares variables that are read-only.
- Variables declared with let can have their values changed, but variables declared with const cannot be reassigned after declaration.
- Both let and const are block-scoped, meaning they are only accessible within the block they are defined in.
- let variables can be declared without an initial value, whereas const variables must be initialised at the time of declaration.
It's worth noting that both let and const are block-scoped and can be used within any block statement (if, for, while, etc.). However, const is used for declaring constants that cannot be reassigned, while let is used for regular variables that can be reassigned within their scope.
It's generally recommended to use const whenever possible, as it helps enforce immutability and makes your code more robust. Use let when you explicitly need to reassign a variable.