Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

JavaScript Array splice() Method

Posted on December 31, 2024December 31, 2024 By Admin No Comments on JavaScript Array splice() Method

The Array.splice() method is a built-in JavaScript function used to add, remove, or replace elements in an array. It modifies the original array directly and is commonly used for tasks requiring precise control over array elements.

Syntax

arr.splice(start, deleteCount, item1, item2, ..., itemN);

Parameters

ParameterDescription
start(Required) The index at which to start changing the array. Negative values count from the end of the array.
deleteCount(Optional) The number of elements to remove from the array. If omitted, all elements from start to the end are removed.
item1, …, itemN(Optional) The elements to add to the array, starting at the start index. If no elements are provided, splice() only remove elements.

Return Value

The splice() method returns an array containing the removed elements. If no elements are removed, it returns an empty array.

Example 1: Removing Elements

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let removed = arr.splice(1, 2);
console.log(removed); // Output: [ 20, 30 ]
console.log(arr); // Output: [ 10, 40, 50 ]
let arr = [10, 20, 30, 40, 50]; let removed = arr.splice(1, 2); console.log(removed); // Output: [ 20, 30 ] console.log(arr); // Output: [ 10, 40, 50 ]
let arr = [10, 20, 30, 40, 50];
let removed = arr.splice(1, 2);
console.log(removed); // Output: [ 20, 30 ]
console.log(arr);  // Output: [ 10, 40, 50 ]

Example 2: Adding Elements

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 40, 50];
arr.splice(1, 0, 20, 30);
console.log(arr); // Output: [10, 20, 30, 40, 50]
let arr = [10, 40, 50]; arr.splice(1, 0, 20, 30); console.log(arr); // Output: [10, 20, 30, 40, 50]
let arr = [10, 40, 50];
arr.splice(1, 0, 20, 30);
console.log(arr); // Output: [10, 20, 30, 40, 50]

Example 3: Replacing Elements

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
arr.splice(1, 2, 100, 200);
console.log(arr);
// Output: [ 10, 100, 200, 40, 50 ]
let arr = [10, 20, 30, 40, 50]; arr.splice(1, 2, 100, 200); console.log(arr); // Output: [ 10, 100, 200, 40, 50 ]
let arr = [10, 20, 30, 40, 50];
arr.splice(1, 2, 100, 200);
console.log(arr); 
// Output: [ 10, 100, 200, 40, 50 ]

Example 4: Removing All Elements from a Starting Index

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
let removed = arr.splice(2);
console.log(removed); // Output: [ 30, 40, 50 ]
console.log(arr); // Output: [ 10, 20 ]
let arr = [10, 20, 30, 40, 50]; let removed = arr.splice(2); console.log(removed); // Output: [ 30, 40, 50 ] console.log(arr); // Output: [ 10, 20 ]
let arr = [10, 20, 30, 40, 50];
let removed = arr.splice(2);
console.log(removed); // Output: [ 30, 40, 50 ]
console.log(arr); // Output: [ 10, 20 ]

Example 5: Using Negative Indices

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
arr.splice(-2, 1);
console.log(arr);
// Output: [ 10, 20, 30, 50 ]
let arr = [10, 20, 30, 40, 50]; arr.splice(-2, 1); console.log(arr); // Output: [ 10, 20, 30, 50 ]
let arr = [10, 20, 30, 40, 50];
arr.splice(-2, 1);
console.log(arr); 
// Output: [ 10, 20, 30, 50 ]

Example 6: Clearing an Array

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [10, 20, 30, 40, 50];
arr.splice(0, arr.length);
console.log(arr); // Output: []
let arr = [10, 20, 30, 40, 50]; arr.splice(0, arr.length); console.log(arr); // Output: []
let arr = [10, 20, 30, 40, 50];
arr.splice(0, arr.length);
console.log(arr); // Output: []

Supported Browsers

BrowserSupport
Chrome1+
Firefox1+
Safari1+
Edge12+
Opera4+
Internet Explorer5.5+
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array some() Method
Next Post: JavaScript Array toReversed() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • July 2025
  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • Interview Experience
  • Java
  • JavaScript
  • Lodash
  • PHP
  • Programs
  • Python
  • Software Testing
  • Web Technologies
  • Web Templates

Recent Posts

  • Java Program to Find the Second Largest Element of an Array
  • Adobe Interview Experience | Senior QA Engineer | Automation Test for Noida Office [Latest 2025]
  • Java Program to Find the Largest Element of an Array
  • Java Program to Calculate Average of Numbers Using Arrays
  • JavaScript Array isArray() Method

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme