About Lesson
JavaScript Array Const
When you declare an array using const
, the reference to the array cannot be reassigned. However, the array elements can still be modified.
Example:
const fruits = ["Apple", "Banana", "Orange"];
// Modify array elements
fruits[0] = "Mango";
console.log(fruits); // Output: ["Mango", "Banana", "Orange"]
Even though fruits
is declared as a constant, its elements can be changed. However, you cannot assign a new array to fruits
once it’s declared.
Example:
const fruits = ["Apple", "Banana", "Orange"];
// This will throw an error
fruits = ["Mango", "Pineapple"];
Using const
with arrays is useful when you want to ensure that the reference to the array remains constant, but you need to modify its elements.