Skip to content
the poor coder

javascript array splice example

Poor Man
Published
Reading time
2 min read
On this page

JavaScript Array Splice Example#

JavaScript array splice is a method that allows you to add or remove elements from an array. It can be used to modify an array in place, without creating a new array. In this post, we will discuss the JavaScript array splice method in detail with the help of examples.

Syntax#

The syntax for the JavaScript array splice method is:

Text
array.splice(index, howMany, [element1], [element2], ..., [elementN])
  • index - The index at which to start changing the array.
  • howMany - The number of elements to remove from the array. If set to 0, no elements are removed.
  • element1...elementN - The elements to add to the array, beginning at the index.

Examples#

Let's take a look at some examples of using the JavaScript array splice method.

Removing Elements from an Array

Suppose we have an array fruits containing some fruits:

Text
var fruits = ['apple', 'banana', 'orange', 'grape', 'kiwi'];

If we want to remove elements from this array, we can use the splice() method. For example, to remove two elements starting from the index 2 (orange and grape), we can do:

Text
// Remove two elements from index 2
fruits.splice(2, 2);

The resulting array will be:

Text
['apple', 'banana', 'kiwi']

Adding Elements to an Array

We can also use the splice() method to add elements to an array. For example, if we want to add two elements ('orange' and 'grape') starting from the index 2, we can do:

Text
// Add two elements from index 2
fruits.splice(2, 0, 'orange', 'grape');

The resulting array will be:

Text
['apple', 'banana', 'orange', 'grape', 'kiwi']

Replacing Elements in an Array

We can also use the splice() method to replace elements in an array. For example, if we want to replace the element at index 2 with a new element ('mango'), we can do:

Text
// Replace element at index 2
fruits.splice(2, 1, 'mango');

The resulting array will be:

Text
['apple', 'banana', 'mango', 'grape', 'kiwi']

Multiple Ways to Remove Elements from an Array

There are multiple ways to remove elements from an array using the splice() method. For example, to remove the last element from an array, we can do:

Text
// Remove last element
fruits.splice(-1);

To remove all elements from an array, we can do:

Text
// Remove all elements
fruits.splice(0, fruits.length);

These are just a few examples of using the splice() method. With its flexibility, you can manipulate arrays in many ways.

Conclusion#

The JavaScript array splice method is a powerful tool for adding, removing, and replacing elements in an array. It can help you to manipulate arrays in many ways, depending on your needs. Use these examples to get started, and experiment with the splice() method to see what you can accomplish.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC