The `const` keyword in JavaScript is used to declare variables that are block-scoped, similar to `let`, but with one key difference: the value of a `const` variable cannot be reassigned once it is set. This makes `const` useful for declaring constants or variables that should not be modified after their initial assignment.
Key Characteristics of `const`:
1. Block Scope:
Like `let`, `const` is block-scoped, meaning it is only accessible within the block it is defined.
2. No Re-assignment:
Variables declared with `const` cannot be reassigned a new value. However, if the variable is an object or array, its properties or elements can be modified.
3. Must Be Initialized:
A `const` variable must be initialized at the time of declaration.
Example 1: Basic Usage
const pi = 3.14159;
console.log(pi); // Output: 3.14159
pi = 3.14; // Error: Assignment to constant variable.
Here, the constant `pi` is initialized with the value `3.14159`. Attempting to reassign a new value to `pi` results in an error.
Example 2: Block Scope
if (true) {
const maxAge = 100;
console.log(maxAge); // Output: 100
}
console.log(maxAge); // Error: maxAge is not defined
The `const` variable `maxAge` is defined within an `if` block and is only accessible within that block. Trying to access it outside the block results in an error.
Example 3: Arrays and Objects
While `const` prevents reassignment, it does not make objects and arrays immutable. You can still change the contents of an array or the properties of an object.
const numbers = [1, 2, 3];
numbers.push(4);
console.log(numbers); // Output: [1, 2, 3, 4]
numbers = [5, 6, 7]; // Error: Assignment to constant variable.
const person = { name: "Alice", age: 30 };
person.age = 31;
console.log(person); // Output: { name: "Alice", age: 31 }
person = { name: "Bob", age: 25 }; // Error: Assignment to constant variable.
In this example, the array `numbers` and the object `person` are declared with `const`. The contents of `numbers` and the properties of `person` can be modified, but the variable itself cannot be reassigned to a new array or object.
Example 4: Temporal Dead Zone
Similar to `let`, `const` variables are also subject to the temporal dead zone.
{
console.log(temp); // Error: Cannot access 'temp' before initialization
const temp = "hello";
console.log(temp); // Output: hello
}
Accessing the `const` variable `temp` before it is declared results in a `ReferenceError`.
Example 5: No Re-declaration within the Same Scope
const x = 10;
const x = 20; // Error: Identifier 'x' has already been declared
Re-declaring a `const` variable in the same scope results in an error.
Example 6: Const in Loops
Using `const` in loops can be useful when you want to ensure the loop variable is not reassigned.
for (const i = 0; i < 3; i++) {
console.log(i); // Error: Assignment to constant variable.
}
Here, trying to increment the loop variable `i` results in an error because `i` is declared with `const`. However, `const` can be used within each iteration of a loop when the variable should be block-scoped and not reassigned.
const arr = [1, 2, 3];
for (const num of arr) {
console.log(num); // Output: 1, 2, 3
}
In this case, `num` is block-scoped within each iteration of the loop and prints the elements of the array without causing an error.
Summary
- `const` variables are block-scoped and cannot be reassigned.
- They must be initialized at the time of declaration.
- The contents of objects and arrays declared with `const` can be modified, but the variable itself cannot be reassigned.
- `const` helps in writing more predictable and error-free code by preventing unintentional reassignments.