AI/ML Software Consulting and Development

Back to blog

JavaScript Array Methods: .splice() vs .slice()

JavascriptWeb Development

Complimentary Consultation

We will explore how you can optimise your digital solutions and software development needs.


We’ve decided to take a “programming” topic for a change and fill in some blank places in JavaScript development topic Specifically we’ll describe Array Splice and Slice methods, how you can use this stuff, and what the difference between them.

JavaScript arrays are frequently used to store different types of data. For example, images for galleries, banners, temporary user data, etc. Array.slice and array.splice are those two frequently used methods that will be required to access and change data inside those arrays.


First let’s take a look at slice method. It returns a shallow copy of a portion of an array into a new array. To have the ability to take that portion array.slice can work with two arguments (see the Difference #3).


So, the next function we’ll gonna look at is the splice function. It removes existing data element and/or adds a new item to the array. And this method changes the contents of the original arrays.

Count from Zero

Now, we’ll take a little pause before examining code samples. Always remember that arrays in JavaScript are zero-based. Meaning that JavaScript starts counting data in the array from number 0. E.g. zero fingers, first finger, second finger, etc.

Remember that arrays in JavaScript are zero-based.

Therefore when you write a starting position for Array.slice method you should start counting from zero, or just add one ahead after you’ve made a count.

JavaScript  Array Splice vs Slice Difference

Difference #1

As you may note the main differences between Array.slice and Array.splice methods are that Slice does not affect the original array. On the other hand, splice method gives the ability to edit initial array.

Array.slice method does not affect the original array.

So if you’d make a couple of slices of the original array it will stay intact. Opposite to the result, you get if you use Array.splice method.

Difference #2

The second difference is that using Splice you can return an item to the original array. The slice method can return selected objects too, but it returns selected items in the array as a new object.

Difference #3

Array.slice can take two arguments. The first one is gonna be your starting index and the second will be your optional end index.

The 1st argument is required. It marks the starting point of ‘splicing’ (please remember about zero-based count).

The 2nd argument is optional. If the second argument is missing, it will take all the element from the starting argument right to the end of the array.

var array=[orange,banana,tomato,potato,onion]

// gives [tomato, potato, onion]

You can also use negative numbers as arguments. This way you’ll select data from the end of the array.

// gives [potato, onion],

// gives [orange, banana, tomato, potato, onion], with original array remains intact.

More examples:

var array2=[3,4,5,6,0];
// gives [5, 6]

// gives [6]

// gives [5, 6]

// gives [3,4,5,6,0]

Now if you place an argument NaN, it will be treated as 0

var array3=[orange,banana,tomato,potato,onion];
// gives nothing [ ]

// gives [orange,banana,tomato,potato,]

// gives [orange,banana,tomato,potato,onion]

One more thing to notice. If you use the arguments that are greater than the array’s length, then it will work with data only from array’s legth.

var array4=[orange,banana,tomato,potato,onion];
// gives [ ]

// gives [ ]

// gives [tomato,potato,onion], until the end of the array as if the second argument is omitted.

// gives [orange,banana,tomato,potato,onion]

Difference #4

The last difference is that Array.splice method can take n number of arguments:

  • 1st argument is required. It specifies what position to add/remove items. Use negative values to specify the position from the end of the array.
  • 2nd argument is optional. It indicates the number of items that should be removed. If 2nd argument is set to 0 (zero), the items won’t be removed. If the 2nd argument is omitted or not passed, all item(s) from provided index will be removed.
  • 3rd argument is optional. It indicates the new item(s) that should be added to the array.
var array=[orange,banana,tomato,potato,onion];
// gives [tomato,potato,onion], it returns removed item(s) as a new array object.

// gives [orange,banana], original array is altered.

var array2=[orange,banana,tomato,potato,onion];
// gives [tomato]

//returns nothing [ ] , because no item(s) was removed.

// gives array [orange,banana,potato,onion] without tomato

var array3=[orange,banana,tomato,potato,onion];
// gives [tomato]

// gives [orange,banana,"Hello","World",potato,onion]

              -5     -4     -3    -2      -1
               |      |      |     |       |
var array4=[orange,banana,tomato,potato,onion];
               |      |      |     |       |
               0      1      2     3       4

// gives back [potato]

// gives back array with “me” insted of potato [orange,banana,tomato,"me",onion]

If the 1st argument is NaN, it will be treated as if it is 0.

var array5=[orange,banana,tomato,potato,onion];
console.log(array5.splice(NaN,4,"NaN is Treated as 0"));
// gives [orange,banana,tomato,potato,]

// gives ["NaN is Treated as 0",onion]

If 2nd argument is less than 0 or equal to NaN, it is treated as if it were 0.

var array6=[orange,banana,tomato,potato,onion];
// gives nothing [ ]

// inserts “Hello” into the array [orange,banana,"Hello",tomato,potato,onion]

// gives nothing [ ]

// but inserts “World” [orange,banana,"Hello","World",tomato,potato,onion]

If the 1st argument or the 2nd argument is greater than Array’s length, each of arguments will use the Array’s length only.

var array7=[orange,banana,tomato,potato,onion];
console.log(array7.splice(23,3,"Add This"));
// gives nothing [ ]

// adds “Add This” at the end [orange,banana,tomato,potato,onion,"Add This"]

console.log(array7.splice(2,34,"Add This Item"));
// gives [tomato,potato,onion,"Add This"]

// gives [orange,banana,"Add This Item"]

Hope this little article helps you with your project. Good luck and contact us if any web development services are needed!

Sign up for the latest Altamira news
Latest Articles

Looking forward to your message!

  • Our experts will get back to you within 24h for free consultation.
  • All information provided is kept confidential and under NDA.