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:

["Apple", "Orange", "Mango", "Grapes"]

 Adding and Removing Elements

Output:

["Kiwi", "Apple", "Orange", "Mango"]

 Reassigning the Array (Not Allowed)

✅ You cannot reassign a new array to a const variable.


 Key Points

  1. const protects the reference to the array.

  2. You can modify elements inside the array.

  3. 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

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *