In JavaScript, you can remove an element from an array at a specific index using the splice()
method.
The splice()
method can be used to add or remove elements from an array. To remove an element, you need to specify the starting index and the number of elements to remove.
Here’s an example that shows how to remove an element at a specific index:
let arr = ["apple", "banana", "orange", "mango"];
let indexToRemove = 2; // index of 'orange'
arr.splice(indexToRemove, 1);
console.log(arr); // Output: ['apple', 'banana', 'mango']
In the above example, the splice()
method is used to remove the element at index 2 (which is the third element 'orange'
) from the arr
array. The second parameter 1 specifies that only one element should be removed.
Note that the splice()
method modifies the original array, so make a copy of the array if you need to keep the original array intact.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.