JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Array Const


📘 JavaScript Array constConstant Reference, Not Constant Content

When you declare an array using const, you cannot reassign the array variable, but you can modify its contents (i.e., add, remove, or change items).

This often confuses beginners who think const makes the whole array unchangeable — it does not.


🔹 Declaring Array with const

const fruits = ["Apple", "Banana", "Mango"];
  • ✅ You can modify items or use array methods:

fruits[0] = "Orange";     // OK
fruits.push("Grapes");    // OK
  • ❌ You cannot reassign the variable:

fruits = ["New", "Array"]; // ❌ TypeError: Assignment to constant variable

🔹 Why Use const with Arrays?

  • Prevents accidental reassignment of the variable.

  • Still allows safe modification of the array contents.

  • Improves code readability and safety in many situations.


🔹 Example
const numbers = [1, 2, 3];

numbers[1] = 99;         // Modify element
numbers.push(4);         // Add element
console.log(numbers);    // [1, 99, 3, 4]

numbers = [10, 20];      // ❌ Error (Cannot reassign const)

⚠️ Common Mistakes

Mistake Explanation
Reassigning a const array ❌ Not allowed
Thinking const makes it immutable ❌ Only reference is constant, not the content

Practice Questions

Q1. How do you declare an array with constant reference in JavaScript?

Q2. Can you change the values inside a const declared array?

Q3. What happens if you try to reassign a new array to a const variable?

Q4. Is it allowed to use .push() or .pop() on a const array?

Q5. How do you update the second value in const numbers = [1, 2, 3]?

Q6. Can you use splice() on a const array?

Q7. Which method will throw an error with const arrays: push() or reassignment?

Q8. What is the difference between let and const when used with arrays?

Q9. Why is const preferred for arrays in many cases?

Q10. What is the output of modifying a const array’s element?


JS Array Const Quiz

Q1: What does const prevent in arrays?

A. Changing values inside the array
B. Reassigning the array to a new array
C. Using push()
D. Using pop()

Q2: What is allowed with a const array?

A. Reassignment
B. Modifying values
C. Using const.push()
D. Changing type

Q3: Which of the following will cause an error with const arrays?

A. arr[0] = 5;
B. arr.push(10);
C. arr = [1, 2];
D. arr.pop();

Q4: Why might you prefer const over let for declaring arrays?

A. It uses less memory
B. It prevents accidental reassignment
C. It makes arrays faster
D. It is required

Q5: What does const arr = [1, 2, 3]; arr = [4, 5]; cause?

A. Works fine
B. Error
C. Outputs [4, 5]
D. Outputs [1, 2, 3, 4, 5]

Q6: Which of the following is true about const arrays?

A. They are completely immutable
B. They can be reassigned
C. Their reference is fixed
D. They cannot contain duplicate values

Q7: Which statement is true?

A. const makes arrays readonly
B. const only prevents reassignment
C. const prevents .push()
D. const makes arrays faster

Q8: What happens when modifying a value inside a const array?

A. Error
B. Nothing
C. Allowed
D. Skipped

Go Back Top