Categories
JavaScript Answers

How to Remove an Element in an Array in JavaScript?

Spread the love

Sometimes, we want to remove an element in an array with JavaScript.

In this article, we’ll look at how to remove an element in an array with JavaScript.

Use the Array.prototype.splice Method

We can use the JavaScript array splice method to remove an item from an array.

For instance, we can write:

const arr = [1, 2, 3]
arr.splice(1, 1)
console.log(arr)

to remove the item with index 1 from arr with splice .

The first argument of splice is the index of the item to remove.

And the 2nd argument is the number of items to remove starting from the index in the first argument.

Therefore arr is [1, 3] according to the console log.

Conclusion

We can remove an item from a JavaScript array with the JavaScript array’s splice method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *