JavaScript - Replace an Item from JS Array Last Updated : 01 Apr, 2025 Comments Improve Suggest changes Like Article Like Report These are the following ways to replace an item from the JS array: 1. Using Array IndexingThe Array Indexing approach replaces an item in an array by directly accessing its position using the index and assigning a new value. This method is straightforward, modifies the original array, and is ideal for known index positions. JavaScript let arr = [1, 2, 3, 4, 5]; const idx = 2; const val = 10; arr[idx] = val; console.log(arr); Output[ 1, 2, 10, 4, 5 ] 2. Using the splice() MethodThe splice() method in JavaScript is another way to replace items in an array. This method allows you to remove items and insert new ones at the specified position in the array.Note: Splice() method deletes zero or more elements of an array starting with including the start_index element and replaces those elements with zero or more elements specified in the argument list. JavaScript let arr = [10, 20, 30]; arr.splice(1, 1, 25); console.log(arr); Output[ 10, 25, 30 ] 3. Using the indexOf() MethodUsing the indexOf() method finds the index of an element in an array, allowing you to perform operations based on its presence or absence. JavaScript const arr = ['a', 'b', 'c']; const idx = arr.indexOf('a'); if (idx !== -1) { arr[idx] = 'z'; } console.log(arr); Output[ 'z', 'b', 'c' ] Comment More infoAdvertise with us Next Article JavaScript - Replace an Item from JS Array akshatyadav Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA Similar Reads JavaScript - Delete an Item From an Array In Javascript, we do not have any array.remove() method for deleting the element. Here are the various methods we can delete an item from an array using JavaScript.Different Ways to Delete an Item From an Array Using JavaScript1. Using for loop and push() MethodThe push() method will not mutate the 4 min read How to Splice Duplicate Item from JavaScript Array? Given an array of numbers or strings containing some duplicate values, the task is to remove these duplicate values from the array without creating a new array or storing the duplicate values anywhere else.Examples: Input: arr = [1, 2, 3, 4, 3, 2, 1];Output: [1,2,3,4]Input: [1, 4, 6, 1, 2, 5, 2, 1, 4 min read How to Remove a Specific Item from an Array in JavaScript ? Given an Array, the task is remove specific item from an array in JavaScript. It means we have an array with N items, and remove a particular item from array.ExamplesInput: Arr = [ 10, 20, 30, 40, 50, 60 ] removeItem = 40 Output: [ 10, 20, 30, 50, 60 ]Table of ContentUsing splice() MethodUsing filte 3 min read Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple", 6 min read JavaScript- Delete from a Given Position of JS Array These are the following ways to delete an item from the given array at given position: 1. Using the splice() Method(Most efficient for in-place modifications)The splice() method is the most versatile way to remove elements from any position in an array. It allows you to specify the index where the r 2 min read JavaScript | Remove a JSON attribute In this article, we will see how to remove a JSON attribute from the JSON object. To do this, there are few of the mostly used techniques discussed. First delete property needs to be discussed. Delete property to Remove a JSON Attribute This keyword deletes a property from an object: This keyword de 2 min read JavaScript - Delete last Occurrence from JS Array These are the following ways to remove the last Item from the given array: 1. Using pop() Method (Simple and Easiest Method for Any Array) The pop() method is used to get the last element of the given array it can also be used to remove the last element from the given array. JavaScriptlet a = [34, 2 2 min read How to remove falsy values from an array in JavaScript ? Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below falsezero(0,-0)empty string("", ' ' , ` `)BigIntZero(0n,0x0n)nullundefinedNaNIn JavaScript, the array accepts all types of falsy values. Let's see some approaches on how we can remove falsy values from an array in Ja 6 min read JavaScript- Add an Object to JS Array In JavaScript, arrays are used to store multiple values in a single variable, and objects are collections of properties and values. Sometimes, we may need to add an object to an array to manage data more effectively. These are the following ways to add an object to a JS array: Using JavaScript Array 4 min read JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po 3 min read Like