DEV Community

Swarnali Roy
Swarnali Roy

Posted on • Edited on

4 2

Modifying Array Data with Indices

In this episode of the series, let's learn How to Modify Array Data with Indices.

In addition to accessing array data with index, we can also modify them with the same square bracket [] notation. We can set an index with a new value replacing the existing one, with the same notation.

For Example:

let numArr = [50,60,70];
numArr[1] = 20;
console.log(numArr[1]); //output: 20
console.log(numArr); //output: [50,20,70]
Enter fullscreen mode Exit fullscreen mode

In the above example, the value of the index 1 is assigned to the value 20. If we see the console, we will notice that index 1 is now holding the new value 20, replacing the initial value 60. The numArr is holding the value [50,20,70]

Let's see another easy example to modify the same array. In this example, we will add a new index with a new value in the existing array , with the same notation. The new index with new value will be added at the end of the array.

let numArr = [50,60,70];
numArr[3] = 80;
console.log(numArr[3]); //output: 80
console.log(numArr); //output: [50,60,70,80]
Enter fullscreen mode Exit fullscreen mode

We can notice that, the array is initialized with three indices 0,1 and 2 holding the value, 50,60 and 70 respectively. Here, numArr[3] simply added a fourth index to the array assigned with a value of 80. Now if we see the console, the numArr is holding the value [50,60,70,80], adding the fourth index with the value 80.

In the next episode, we will discuss about other methods to modify an array by adding or removing it's elements.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay