JavaScript, How to update an array element
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](https://d33wubrfki0l68.cloudfront.net/0bda70cefb503e04dd9e0e47b08e4500973e498f/c59e4/update-an-array-element-javascript/update-array-element_hu2f03198da25c1970b2b19171aa11438f_11109_e17d7c66c814fd63f5a7cb788fb49d9c.webp)
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](https://d33wubrfki0l68.cloudfront.net/2ca7acd9469b6c3d13511968c3eceded713ffee2/00876/update-an-array-element-javascript/update-array-element-searching-by-value-with-index-of_hu4037f8851d950baa35f7e8ad6380bbc2_18027_aaeada40e26f8869d5af8cffb14f537f.webp)
Hope you like this!
Keep helping and happy 😄 coding