Replace an Element in an Array in JavaScript-Code Examples

Radu TM

Replace an Element in an Array in JavaScript

JavaScript by Radu TM β€’ June 17, 2022

// this is an array of emoji fruits
var emojiFruits = ["πŸ‰", "🍊", "πŸ‹", "🍌", "🍍", "🍎", "🍏", "🍐", "πŸ’", "πŸ“"];

// this is the element we want to replace
var oldElement = "πŸ‰";

// this is the element we want to replace it with
var newElement = "πŸ‡";

// this is the index of the element we want to replace
var index = 0;

// replace the element at the specified index with the new element
emojiFruits[index] = newElement;

// log the updated array to the console
console.log(emojiFruits);

0

13.431

Replace an Element in an Array in JavaScript

JavaScript by Radu TM β€’ June 17, 2022

// array of emoji fruits
var fruits = ["🍎", "🍊", "πŸ‹"];

// replace the element at index 1 (the second element) with "🍌" (banana)
fruits[1] = "🍌"; 

console.log(fruits); // ["🍎", "🍌", "πŸ‹"]

0

13.431

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories