🌐
W3Schools
w3schools.com › jsref › jsref_splice.asp
JavaScript Array splice() Method
// Create an Array const fruits = ["Banana", "Orange", "Apple", "Mango"]; // At position 2, add "Lemon" and "Kiwi": fruits.splice(2, 0, "Lemon", "Kiwi"); Try it Yourself » · More Examples Below !
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › splice
Array.prototype.splice() - JavaScript | MDN
If start >= array.length, no element will be deleted, but the method will behave as an adding function, adding as many elements as provided. If start is omitted (and splice() is called with no arguments), nothing is deleted.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-splice-method
JavaScript Array splice() Method - GeeksforGeeks
If omitted, all elements from startIndex to the end are removed. If set to 0, no elements are removed. item1, item2, ..., itemN: Elements to add starting at startIndex. If none are provided, splice...
Published   October 10, 2018
🌐
Mimo
mimo.org › glossary › javascript › splice
JavaScript Splice Method: Array Modification Techniques
Learn HTML, CSS, JavaScript, and React as well as NodeJS, Express, and SQL ... Master the language of the web. Learn variables, functions, objects, and modern ES6+ features ... const months = ['Jan', 'March', 'April', 'June']; months.splice(1, 2); // Removes 2 elements starting from index 1 ...
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript array splice method
JavaScript Array Splice Method
September 1, 2008 - In the following example, we are using the JavaScript Array splice() method to remove all the array elements, starting from the index postion 2.
🌐
PHP
php.net › manual › en › function.array-splice.php
PHP: array_splice - Manual
The returned arrays is the 2nd ... <?php $input = array("red", "green", "blue", "yellow"); print_r(array_splice($input, 3)); // Array ( [0] => yellow ) print_r($input); //Array ( [0] => red [1] => green [2] => blue ) ?> if you want ...
🌐
Refine
refine.dev › home › blog › tutorials › how to use javascript array splice
How to Use JavaScript Array Splice | Refine
September 5, 2024 - Slice in JavaScriptsplice()slice()Key Differences:Splice on Multidimensional ArraysModifying the Outer ArrayChanging Inner ArraysInserting ElementsApplication ExampleKey PointsUsing Splice with Destructuring in JavaScriptBasic Splice + DestructuringExample: Element SwappingExtracting Multiple Elements with DestructuringDestructuring + InsertionWhy Use This?Summary
🌐
freeCodeCamp
freecodecamp.org › news › javascript-splice-how-to-use-the-splice-js-array-method
JavaScript Splice – How to Use the .splice() JS Array Method
April 23, 2021 - When you omit the removeCount parameter, splice() will remove all elements from the start index to the end of the array. The method also allows you to add new elements right after the delete operation. You just need to pass the elements you want to add to the array after the delete count.
🌐
DhiWise
dhiwise.com › post › javascript-array-splice-method-a-detailed-exploration
JavaScript Array Splice Method: A Comprehensive Guide
September 5, 2024 - In this example, the splice method inserts 'fig' at index 2 without removing any elements. JavaScript splice method returns an array containing the deleted elements. If no elements are removed, it returns an empty array.
Find elsewhere
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.splice()
JavaScript Array splice(): Delete, Insert, and Replace Elements
November 6, 2024 - The following picture illustrates how scores.splice(0,3) works: You can insert one or more elements into an array by passing three or more arguments to the splice() method with the second argument is zero.
🌐
Edureka
edureka.co › blog › javascript-array-splice-method
Splice Array in JavaScript | Array.Splice() Method Examples | Edureka
February 25, 2025 - This invoked method will help to change the content of the array. ... Index is an integer value which addresses on what position element/item need to be added or from which position element/item is to be removed. It is required every time in splice array.
Top answer
1 of 16
433

splice() changes the original array whereas slice() doesn't but both of them returns array object.

See the examples below:

var array=[1,2,3,4,5];
console.log(array.splice(2));

This will return [3,4,5]. The original array is affected resulting in array being [1,2].

var array=[1,2,3,4,5]
console.log(array.slice(2));

This will return [3,4,5]. The original array is NOT affected with resulting in array being [1,2,3,4,5].

Below is simple fiddle which confirms this:

//splice
var array=[1,2,3,4,5];
console.log(array.splice(2));

//slice
var array2=[1,2,3,4,5]
console.log(array2.slice(2));


console.log("----after-----");
console.log(array);
console.log(array2);

2 of 16
130

Splice and Slice both are Javascript Array functions.

Splice vs Slice

  1. The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object.

  2. The splice() method changes the original array and slice() method doesn’t change the original array.

  3. The splice() method can take n number of arguments and slice() method takes 2 arguments.

Splice with Example

Argument 1: Index, Required. An integer that specifies at what position to add /remove items, Use negative values to specify the position from the end of the array.

Argument 2: Optional. The number of items to be removed. If set to 0(zero), no items will be removed. And if not passed, all item(s) from provided index will be removed.

Argument 3…n: Optional. The new item(s) to be added to the array.

var array=[1,2,3,4,5];
console.log(array.splice(2));
// shows [3, 4, 5], returned removed item(s) as a new array object.
 
console.log(array);
// shows [1, 2], original array altered.
 
var array2=[6,7,8,9,0];
console.log(array2.splice(2,1));
// shows [8]
 
console.log(array2.splice(2,0));
//shows [] , as no item(s) removed.
 
console.log(array2);
// shows [6,7,9,0]

Slice with Example

Argument 1: Required. An integer that specifies where to start the selection (The first element has an index of 0). Use negative numbers to select from the end of an array.

Argument 2: Optional. An integer that specifies where to end the selection but does not include. If omitted, all elements from the start position and to the end of the array will be selected. Use negative numbers to select from the end of an array.

var array=[1,2,3,4,5]
console.log(array.slice(2));
// shows [3, 4, 5], returned selected element(s).
 
console.log(array.slice(-2));
// shows [4, 5], returned selected element(s).
console.log(array);
// shows [1, 2, 3, 4, 5], original array remains intact.
 
var array2=[6,7,8,9,0];
console.log(array2.slice(2,4));
// shows [8, 9]
 
console.log(array2.slice(-2,4));
// shows [9]
 
console.log(array2.slice(-3,-1));
// shows [8, 9]
 
console.log(array2);
// shows [6, 7, 8, 9, 0]

🌐
Programiz
programiz.com › javascript › library › array › splice
JavaScript Array splice()
If start > array.length, splice() does not delete anything and starts appending arguments to the end of the array. If start < 0, the index is counted from backward (array.length + start). For example, -1 is the last element.
🌐
Javatpoint
javatpoint.com › javascript-array-splice-method
JavaScript Array splice() method
JavaScript Array Method The method creates a new array that holds the shallow copy from an array or iterable object. When applied to a string, each word gets converted to an array element in the new array.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-array_splice-function
PHP array_splice() Function - GeeksforGeeks
September 24, 2024 - <?php // PHP program to illustrate the use // of array_splice() function $array1 = array("10"=>"raghav", "20"=>"ram", "30"=>"laxman","40"=>"aakash","50"=>"ravi"); $array2 = array("60"=>"ankita","70"=>"antara"); echo "The returned array: \n"; ...
🌐
freeCodeCamp
freecodecamp.org › news › javascript-slice-and-splice-how-to-use-the-slice-and-splice-js-array-methods
How to Use the slice() and splice() JavaScript Array Methods
April 13, 2022 - Since we are not deleting any items, our delete count is zero. This is what the result would look like in the console. const food = ['pizza', 'cake', 'salad', 'cookie']; food.splice(1,0,"burrito") console.log(food)
🌐
Tutorialspoint
tutorialspoint.com › home › php › php array_splice function
PHP array_splice Function
May 26, 2007 - <?php $input = array("red", "black", "pink", "white"); array_splice($input, 2); print_r($input); print_r("<br />"); $input = array("red", "black", "pink", "white"); array_splice($input, 1, -1); print_r($input); print_r("<br />"); $input = ...
🌐
CodingNomads
codingnomads.com › javascript-array-splice-insert-delete-replace
How to Replace, Add or Delete Array Items: JavaScript Splice
One of the most common JavaScript array operations is to add and remove elements from them. In this lesson, you'll focus in on the particularly versatile .splice() method that can be tailored to suit many situations involving array manipulation.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Array methods
In the next example, we remove 3 elements and replace them with the other two: let arr = ["I", "study", "JavaScript", "right", "now"]; // remove 3 first elements and replace them with another arr.splice(0, 3, "Let's", "dance"); alert( arr ) ...