-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
const
– Constant Reference, Not Constant ContentWhen 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.
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
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.
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)
Mistake | Explanation |
---|---|
Reassigning a const array |
❌ Not allowed |
Thinking const makes it immutable |
❌ Only reference is constant, not the content |
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?
Q1: What does const prevent in arrays?
Q2: What is allowed with a const array?
Q3: Which of the following will cause an error with const arrays?
Q4: Why might you prefer const over let for declaring arrays?
Q5: What does const arr = [1, 2, 3]; arr = [4, 5]; cause?
Q6: Which of the following is true about const arrays?
Q7: Which statement is true?
Q8: What happens when modifying a value inside a const array?