Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

Splice() method in Javascript

- 28.09.21 - ErcanOPAK

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, you should use the slice() method.

Syntax:

splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)

Parameters

start
The index at which to start changing the array.

If greater than the length of the array, start will be set to the length of the array. In this case, no element will be deleted but the method will behave as an adding function, adding as many element as item[n*] provided.

If negative, it will begin that many elements from the end of the array. (In this case, the origin -1, meaning -n is the index of the nth last element, and is therefore equivalent to the index of array.length - n.) If start is negative infinity, it will begin from index 0.

 

deleteCount Optional
An integer indicating the number of elements in the array to remove from start.

If deleteCount is omitted, or if its value is equal to or larger than array.length - start (that is, if it is equal to or greater than the number of elements left in the array, starting at start), then all the elements from start to the end of the array will be deleted.

If deleteCount is 0 or negative, no elements are removed. In this case, you should specify at least one new element.

 

item1, item2, ... Optional
The elements to add to the array, beginning from start. If you do not specify any elements, splice() will only remove elements from the array.

Return Value:
An array containing the deleted elements.
If only one element is removed, an array of one element is returned.
If no elements are removed, an empty array is returned.

 

Examples:

let myArray = ['A', 'B', 'C', 'D', 'E']

let removed = myArray.splice(1, 0, 'F')
//1 means: Find 2nd value in array. 
//0 means: Don't delete anything. You will add the 3rd parameter which is 'F' to the place of 2nd value of array (the place of 'C') 
//'F' means: Add this

// myArray is ['A', 'B', 'F', 'C', 'D', 'E']
// removed is [], no elements removed

-------------------------------------------------------------

let myArray = ['A', 'B', 'C', 'D', 'E']
let removed = myArray.splice(4, 0, 'G', 'S')
//1 means: Find 5th value in array. 
//0 means: Don't delete anything. You will add the 3rd and 4th parameters which are 'G' and 'S' to the place of 5th value of array (the place of 'E') 
//'G', 'S' means: Add these

// myArray is ['A', 'B', 'C', 'D', 'G', 'S', 'E']
// return value, removed is [], no elements removed

-------------------------------------------------------------

let myArray = ['A', 'B', 'C', 'D', 'E']
let removed = myArray.splice(3, 1)
//3 means: Find 4th value in array. 
//1 means: Start to delete from 4th value. And delete only 1 value 

// myArray is ['A', 'B', 'C', 'E']
// return value, removed is ['D']

-------------------------------------------------------------

let myArray = ['A', 'B', 'C', 'D', 'E']
let removed = myArray.splice(1, 1, 'F')
//2 means: Find 3rd value in array. 
//1 means: Start to delete from 3rd value. And delete only 1 value
//'F' means: Delete 'B' and add this 'F' instead of it 

// myArray is ['A', 'F', 'C', 'D', 'E']
// return value, removed is ['B']

-------------------------------------------------------------

 

Related posts:

Ajax Forms Submit Twice

Ajax Forms Submit Twice

JavaScript: Use ?? Instead of || for Default Values

JavaScript: Use ?? Instead of || for Default Values

JavaScript: Fix an async Function Inside Array.prototype.map() Silently Returning an Array of Pendin...

JavaScript: Fix an async Function Inside Array.prototype.map() Silently Returning an Array of Pendin...

Views: 24

Post navigation

IndexOf() and LastIndexOf() in C#
Slice() method in Javascript

Leave a Reply Cancel reply

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

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (478)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com