Skip to content

Latest commit

 

History

History
127 lines (80 loc) · 4.29 KB

File metadata and controls

127 lines (80 loc) · 4.29 KB

Explain slice() and splice() Methods of an Array

The slice() and splice() methods are two commonly used array methods in JavaScript. Both are used to manipulate arrays, but they serve different purposes and operate in distinct ways.

1. slice() Method

Definition:

The slice() method is used to extract a portion of an array without modifying the original array. It returns a new array containing the selected elements.

Syntax:

array.slice(start, end);

Parameters:

  • start (optional): The index at which to begin extraction. If omitted, it defaults to 0.
  • end (optional): The index at which to stop extraction. The end index is not included in the extracted portion. If omitted, it extracts up to the end of the array.

Characteristics:

  • It does not modify the original array.
  • The returned array is a shallow copy of the original array.

Example:

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

// Extracting elements from index 1 to 3 (3 is not included)
const slicedFruits = fruits.slice(1, 3);

console.log(slicedFruits); // Output: ["banana", "cherry"]
console.log(fruits); // Output: ["apple", "banana", "cherry", "date", "elderberry"] (unchanged)

Use Cases:

  • Creating a copy of an array: const copy = array.slice();
  • Extracting specific parts of an array without altering the original.

2. splice() Method

Definition:

The splice() method is used to add, remove, or replace elements in an array. It modifies the original array and returns an array containing the removed elements (if any).

Syntax:

array.splice(start, deleteCount, item1, item2, ...)

Parameters:

  • start: The index at which to begin modifying the array.
  • deleteCount (optional): The number of elements to remove from the array. If 0, no elements are removed.
  • item1, item2, ... (optional): Elements to add to the array at the start index. If omitted, no elements are added.

Characteristics:

  • It modifies the original array.
  • Returns an array of the removed elements.

Example 1: Removing Elements

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

// Removing 2 elements starting from index 1
const removedFruits = fruits.splice(1, 2);

console.log(removedFruits); // Output: ["banana", "cherry"]
console.log(fruits); // Output: ["apple", "date", "elderberry"]

Example 2: Adding Elements

const fruits = ["apple", "date", "elderberry"];

// Adding "banana" and "cherry" at index 1
fruits.splice(1, 0, "banana", "cherry");

console.log(fruits); // Output: ["apple", "banana", "cherry", "date", "elderberry"]

Example 3: Replacing Elements

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

// Replacing 2 elements starting from index 1 with "grape" and "kiwi"
fruits.splice(1, 2, "grape", "kiwi");

console.log(fruits); // Output: ["apple", "grape", "kiwi", "date", "elderberry"]

Use Cases:

  • Removing specific elements from an array.
  • Inserting new elements into an array.
  • Replacing elements within an array.

Key Differences Between slice() and splice()

Feature slice() splice()
Purpose Extracts a portion of an array. Adds, removes, or replaces elements in an array.
Modifies Original? No Yes
Return Value A new array containing the extracted elements. An array of the removed elements.
Parameters start, end start, deleteCount, item1, item2, ...

Summary

  • Use slice() when you need to create a subset of an array without altering the original array.
  • Use splice() when you need to modify the array by adding, removing, or replacing elements.

Both methods are powerful tools for array manipulation, but their appropriate use depends on whether you want to preserve or modify the original array.