remove an element from an array at a specific index in javascript

To remove an element from an array at a specific index in JavaScript, we can use the splice() method. The splice() method changes the contents of an array by removing existing elements and/or adding new elements.

The syntax for using splice() to remove an element from an array at a specific index is as follows:

index.tsx
array.splice(index, 1);
24 chars
2 lines

In the above syntax, index is the index of the element that needs to be removed and 1 indicates the number of elements to be removed from that index.

Here's an example:

index.tsx
let arr = ["apple", "banana", "grape", "orange"];
let indexToRemove = 1;

arr.splice(indexToRemove, 1);

console.log(arr); // Output: ["apple", "grape", "orange"]
163 chars
7 lines

In the above example, we remove the element at index 1 from the arr array. The resulting array is ["apple", "grape", "orange"] after the removal.

gistlibby LogSnag