javascript splice() method


  Translation results:

UK[splaɪs] 美[splaɪs]

vt. splicing; twisting (two sections of rope); gluing; bonding (film, tape, etc.)

n. Glue Joint, bonding, hinge

Third person singular: splices Present participle: splicing Past tense: spliced ​​Past participle: spliced

javascript splice() methodsyntax

How to use the splice() method?

The splice() method directly modifies the array and can be used to insert, delete or replace elements of the array.

Function: Add/remove items to/from the array, and then return the deleted item.

Syntax: arrayObject.splice(index,howmany,item1,...,itemX)

Parameters: index Required. An integer specifying the position at which to add/remove an item. Use a negative number to specify the position from the end of the array. howmany Required. The number of items to delete. If set to 0, items will not be deleted. item1, ..., itemX are optional. New items added to the array.​

Returns: Array A new array containing the deleted items, if any.

Description: The splice() method can delete zero or more elements starting from index and replace those deleted with one or more values ​​declared in the parameter list element. If an element is deleted from arrayObject, an array containing the deleted element is returned.

Note: This method will change the original array.

javascript splice() methodexample

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script type="text/javascript">

    var arr = new Array(6)
    arr[0] = "George"
    arr[1] = "John"
    arr[2] = "Thomas"
    arr[3] = "James"
    arr[4] = "Adrew"
    arr[5] = "Martin"

    document.write(arr + "<br />")
    arr.splice(2,0,"William")
    document.write(arr + "<br />")

</script>

</body>
</html>

Run instance »

Click the "Run instance" button to view the online instance

Home

Videos

Q&A