How to create a constant array in JavaScript? Can we change its values? Explain.

To create a const array in JavaScript, we use the const keyword before the array name. While the array reference cannot be reassigned, individual elements can be modified.

A const array prevents reassignment of the entire array but allows mutation of its contents. This is because const creates an immutable binding, not an immutable value.

Syntax

const arrayName = [element1, element2, element3];

Example: Creating and Modifying a Const Array

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Const Array Example</title>
    <style>
        body {
            font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
        }
        .result, .sample {
            font-size: 18px;
            font-weight: 500;
            margin: 10px 0;
        }
        .result {
            color: green;
        }
        button {
            padding: 10px 15px;
            background-color: #007bff;
            color: white;
            border: none;
            cursor: pointer;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <h1>Const Array in JavaScript</h1>
    <div class="sample"></div>
    <div class="result"></div>
    <button class="btn">CLICK HERE</button>
    <h3>Click the button to modify array elements and attempt reassignment</h3>
    
    <script>
        let sampleEle = document.querySelector(".sample");
        let resEle = document.querySelector(".result");
        const arr = [22, 33, 44, 55];
        
        sampleEle.innerHTML = "Original Array = [" + arr.join(", ") + "]";
        
        document.querySelector(".btn").addEventListener("click", () => {
            // Modifying individual elements (allowed)
            arr[0] = 44;
            arr[1] = 99;
            arr[2] = 0;
            
            resEle.innerHTML += "After modifying elements: [" + arr.join(", ") + "]<br>";
            
            // Attempting to reassign the entire array (will throw error)
            try {
                arr = [11, 11, 11, 11];
            } catch (err) {
                resEle.innerHTML += "Error when reassigning: " + err.message;
            }
        });
    </script>
</body>
</html>

Key Points

Operation Allowed? Result
Modify elements: arr[0] = 10 Yes Elements change successfully
Add elements: arr.push(6) Yes New elements added
Reassign array: arr = [1, 2, 3] No TypeError: Assignment to constant variable

Example: Array Methods with Const

<!DOCTYPE html>
<html>
<head>
    <title>Const Array Methods</title>
</head>
<body>
    <div id="output"></div>
    
    <script>
        const numbers = [1, 2, 3];
        let output = document.getElementById("output");
        
        output.innerHTML += "Original: [" + numbers.join(", ") + "]<br>";
        
        // Adding elements
        numbers.push(4, 5);
        output.innerHTML += "After push: [" + numbers.join(", ") + "]<br>";
        
        // Removing elements
        numbers.pop();
        output.innerHTML += "After pop: [" + numbers.join(", ") + "]<br>";
        
        // Sorting
        numbers.sort((a, b) => b - a);
        output.innerHTML += "After sort: [" + numbers.join(", ") + "]<br>";
    </script>
</body>
</html>

Making Arrays Truly Immutable

To create a completely immutable array, use Object.freeze():

<!DOCTYPE html>
<html>
<head>
    <title>Frozen Array</title>
</head>
<body>
    <div id="frozen-output"></div>
    
    <script>
        const frozenArr = Object.freeze([1, 2, 3]);
        let output = document.getElementById("frozen-output");
        
        output.innerHTML += "Original frozen array: [" + frozenArr.join(", ") + "]<br>";
        
        // Attempt to modify (silently fails in non-strict mode)
        frozenArr[0] = 99;
        frozenArr.push(4);
        
        output.innerHTML += "After modification attempts: [" + frozenArr.join(", ") + "]<br>";
        output.innerHTML += "Array remains unchanged!";
    </script>
</body>
</html>

Conclusion

Const arrays in JavaScript allow element modification but prevent reassignment. Use Object.freeze() for complete immutability when needed.

Updated on: 2026-03-15T23:18:59+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements