JavaScript Array Const

javascript array constructor

Rahul Kaklotar
2 min readApr 10, 2023
Array Const In Javascript

In JavaScript, an array is a collection of values, which can be of any data type, such as numbers, strings, or objects. The const keyword is used to declare a variable whose value cannot be reassigned once it has been initialized.

When used together, const and arrays create a variable whose contents cannot be reassigned, but whose values can still be modified.

Here’s an example:

const fruits = ["apple", "banana", "orange"];

console.log(fruits[0]); // "apple"

fruits.push("grape");

console.log(fruits); // ["apple", "banana", "orange", "grape"]

In the example above, we create a constant variable fruits that contains an array of strings. We can access elements in the array using index notation (fruits[0] returns “apple”).

Even though fruits is a constant variable, we can still modify its contents by using array methods like push(), which adds a new element to the end of the array.

However, we cannot reassign a new array to the fruits variable. For example, the following code will result in an error:

const fruits = ["apple", "banana", "orange"];

fruits = ["grape", "kiwi"]; // Error: Assignment to constant variable.

In summary, using const with arrays in JavaScript creates a variable whose contents cannot be reassigned, but whose values can still be modified using array methods.

and You still confused so go with this array chapter.

--

--

Rahul Kaklotar

I'm front-end developer with 4 years of expertise in HTML, CSS, JavaScript, React. Passionate about creating user-friendly websites with a sharp eye for detail.