MeshWorld India LogoMeshWorld.

How to Update an Array Element in JavaScript

Listen to ArticleAI Speech
~1 min read narration
100%
How to Update an Array Element in JavaScript

In JavaScript, developers need that they need to update the value of an array element. It is just as easy as assigning a new value to a variable using an assignment operator. So in this article, we will see it

Update array value with static index

let colours = ["Red", "Orange", "Yellow", "Olive", "Green"];

console.log(colours);
// Output -> ["Red", "Orange", "Yellow", "Olive", "Green"]

colours[2] = "Blue";
console.log(colours);
// Output -> ["Red", "Orange", "Blue", "Olive", "Green"]

Console screenshot for update array element

Update Array Element Update Array Element

Update array value by searching value

let colours = ["Red", "Orange", "Yellow", "Olive", "Green"];

console.log(colours);
// Output -> ["Red", "Orange", "Yellow", "Olive", "Green"]

let index = colours.indexOf("Yellow");
colours[index] = "Blue";
console.log(colours);
// Output -> ["Red", "Orange", "Blue", "Olive", "Green"]

// One-liner
colours[colours.indexOf("Green")] = "Aqua";
console.log(colours);
// Output -> ["Red", "Orange", "Blue", "Olive", "Aqua"]

Console screenshot for an element searching by value with indexOf method

Update Array Element Searching By Value With IndexOf Method Update Array Element Searching By Value With IndexOf Method


Hope you like this!

Keep helping and happy 😄 coding

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive