Slice And Splice In Javascript W3schools

Slice And Splice In Javascript W3schools

5 min read Jun 20, 2024
Slice And Splice In Javascript W3schools

Slice and Splice in JavaScript: A Comprehensive Guide

This article provides a comprehensive guide to understanding and using the slice() and splice() methods in JavaScript. We will explore their functionalities, differences, and practical applications with illustrative examples.

Slice: Extracting a Portion of an Array

The slice() method is used to extract a portion of an array and return a new array containing the extracted elements. It does not modify the original array.


array.slice(start, end);
  • start: The index of the first element to include in the new array. It's optional, and if omitted, it defaults to 0 (beginning of the array).
  • end: The index of the element after the last element to include in the new array. It's also optional. If omitted, the slice will include all elements from start to the end of the array.


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

// Extract elements from index 1 to 3 (exclusive)
const slicedArray1 = numbers.slice(1, 3); // [2, 3]

// Extract all elements from index 2 onwards
const slicedArray2 = numbers.slice(2); // [3, 4, 5]

// Extract a copy of the entire array
const slicedArray3 = numbers.slice(); // [1, 2, 3, 4, 5]

// Negative indices count from the end of the array
const slicedArray4 = numbers.slice(-2); // [4, 5]

Splice: Modifying an Array In-Place

The splice() method allows you to modify an existing array by adding, removing, or replacing elements directly within the array. Unlike slice(), it modifies the original array.


array.splice(start, deleteCount, ...items);
  • start: The index at which to start modifying the array.
  • deleteCount: The number of elements to remove from the array, starting at start.
  • ...items: The elements to be added to the array at start after removing deleteCount elements.


const fruits = ["apple", "banana", "cherry", "date"];

// Remove the second element ("banana")
fruits.splice(1, 1); // fruits: ["apple", "cherry", "date"]

// Remove two elements starting from index 1
fruits.splice(1, 2); // fruits: ["apple", "date"]

// Add two new elements at index 1
fruits.splice(1, 0, "orange", "grape"); // fruits: ["apple", "orange", "grape", "date"]

// Replace the second element with "mango"
fruits.splice(1, 1, "mango"); // fruits: ["apple", "mango", "grape", "date"]

Key Differences:

  • Return value: slice() returns a new array, while splice() returns an array containing the removed elements.
  • Array modification: slice() does not modify the original array, whereas splice() modifies the original array in place.

Choosing the Right Method:

  • Use slice() when you need to create a copy of a portion of the array without altering the original array.
  • Use splice() when you need to modify the original array by adding, removing, or replacing elements directly within it.

By understanding the subtle differences and applications of both slice() and splice(), you can effectively manipulate JavaScript arrays to achieve your desired results.