How to use const with Arrays In Javascript

const variables must be initialized at the time of declaration. const variables have block scope like let.

Example: This example shows the declaration of array using const keyword.

Javascript
const numbers = [1, 2, 3];
console.log(numbers[0]); 
// Modifying the elements of a const array is allowed
numbers.push(4);
console.log(numbers); 

Output
1
[ 1, 2, 3, 4 ]

What does Const Do in JavaScript ?

In JavaScript, const is a keyword used to declare constants. Once assigned, the value of a constant cannot be changed. It provides a way to create read-only variables. Attempting to reassign a const variable will result in a syntax error.

These are the following ways to use const:

Table of Content

  • Declaring a Constant Variable
  • Using const with Object Literal
  • Using const with Arrays

Similar Reads

Declaring a Constant Variable

The const keyword in JavaScript is primarily used for declaring constants. The value of a const variable cannot be reassigned after initialization....

Using const with the Object Literal

const keyword in JavaScript is primarily used for declaring constants. The value of a const variable cannot be reassigned after initialization....

Using const with Arrays

const variables must be initialized at the time of declaration. const variables have block scope like let....