Code has been added to clipboard!

splice

Reading time 2 min
Published Aug 10, 2017
Updated Oct 10, 2019

JavaScript splice: Main Tips

  • The JavaScript splice() method adds or removes items from an array.
  • The original array gets changed by this method.
  • The array.splice JavaScript method an array of the removed items.

Learn Syntax of splice

Here you can see the basic syntax for this JavaScript splice() function:

array.splice(index, howmany, item1, item2...)

The array.splice JavaScript function accepts several parameters in the parentheses. First of all, you have to indicate the index, or the position in which the removal (or addition process) is supposed to begin.

Secondly, you can include an optional parameter called howmany which specifies the number of elements to be eliminated from the array. Parameter is the item which indicates the elements to be added to the array.

Parameter Description
index Required. Defines the position to add or remove elements.
Negative values will count the position from the end of the array.
howmany Not required. Sets the number of items to remove.
0 means nothing will be removed.
item1, item2... Not required. Specifies the items to add.
Example
var pets = ["Cat", "Mouse", "Rat", "Cactus"];
pets.splice(2, 2, "Llama", "Horse");

Using splice

No wonder JavaScript splice() function is considered to be perfectly-suited for array management. The invoked method will help you change the content of arrays: you can either add new elements or remove the ones that are no longer necessary.

Unlike other JavaScript functions that do not affect the original arrays (for example, Javascript slice()), the JS splice() removes or adds elements directly from or to them. However, the method also generates a list of elements that were deleted from the array. If you chose only to add items, the presented array would be empty.

What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen