In modern JavaScript (ES6+), it has become the standard practice to declare arrays using the
const keyword. This does not mean the array is unchangeable, but rather that
the identifier (the name) cannot be reassigned to a different value.
The const keyword defines a constant reference to an array.
Because of this, we can still change the elements of a constant array.
const cars = ["Saab", "Volvo", "BMW"];
// You can change an element:
cars[0] = "Toyota";
// You can add an element:
cars.push("Audi");
You cannot reassign a const array. If you try to give the array variable a
completely new value, JavaScript will throw an error.
const cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Audi"]; // ERROR: Assignment to constant variable.
An array declared with const has block scope. This means an
array declared inside a block {} is not accessible from outside the block.
const cars = ["Saab", "Volvo"];
{
const cars = ["Toyota", "BMW"]; // This is a different array!
}
console.log(cars[0]); // Result: "Saab"
const for arrays const is the standard for declaring arrays in ES6+const arrays are block-scopedconst array in the same scope is not allowed