Skip to main content

Command Palette

Search for a command to run...

How does the JavaScript splice() method work?

Updated
1 min read
How does the JavaScript splice() method work?

The splice() method will remove items from an array. It will return the removed values and it will mutate the original array. It receives two parameters:

  1. The starting index to remove.
  2. How many items will be removed from the starting position.

For example:

const numbers = [1,2,3,4,5]

let removedValues = numbers.splice(0,2)

removedValues // [ 1, 2 ]

numbers // [ 3, 4, 5 ]

In this example, we start on the position 0 of the array and we remove 2 items. In our example we delete the value of the index we are in and one more.

As we can see the splice() method returns the removed values and mutates the original array.

More from this blog

Mario Barceló

8 posts

💻 Frontend Developer. 📱 Tech passionate. 🎲 Board Game lover. 🏈 Football rookie.