JavaScript – Remove Element of Array by Index
To remove an element of an array at specific index in JavaScript, call splice() method on this array and pass the index and 1 (one element to remove) as arguments.
splice() method returns an array containing the removed element.
The syntax to remove an element of array arr
at index index
is
</>
Copy
arr.splice(i, 1)
Example
In the following example, we have taken an array in arr
. We remove the element from this array present at index 2
using splice() method.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div id="output"></div>
<script>
var arr = ['apple', 'banana', 'cherry', 'mango'];
var index = 2;
arr.splice(index, 1);
document.getElementById('output').innerHTML = 'Output Array : ' + arr;
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned how to remove an element from an array at specific index using splice() method.