JavaScript Arrays and const
JavaScript Arrays and const
In JavaScript, you can declare an array using var, let, or const. Using const means the variable reference cannot be reassigned, but the contents of the array can still be modified.
Example 1: Declaring Array with const
Modifying Array Elements
Even though the array is const, you can change its contents:
Output:
Adding and Removing Elements
Output:
Reassigning the Array (Not Allowed)
✅ You cannot reassign a new array to a const variable.
Key Points
-
constprotects the reference to the array. -
You can modify elements inside the array.
-
You cannot reassign the array variable itself.
Example: Safe Usage of const Array
Summary Table:
| Action | Result with const Array |
|---|---|
| Modify elements | ✅ Allowed |
| Add elements | ✅ Allowed |
| Remove elements | ✅ Allowed |
| Reassign array | ❌ Not Allowed |
