javascript slice() method


  Translation results:

英[slaɪs] 美[slaɪs]

vt. Cut into slices; cut off; divide

n. Thin slice; part; (a curveball hit due to a mistake)

vi. Slash

Third person singular: slices Plural: slices Present participle: slicing Past tense: sliced ​​Past participle: sliced

javascript slice() methodsyntax

How to use the slice() method?

The slice() method can create a new array based on the current array, and will not have any impact on the original array.

Function: Return the selected element from the existing array.

Syntax: arrayObject.slice(start,end)

##Parameters: start Required. Specifies where to start the selection. If negative, it specifies the position from the end of the array. That is, -1 refers to the last element, -2 refers to the second to last element, and so on. end Optional. Specifies where the selection ends. This parameter is the array index at the end of the array fragment. If this parameter is not specified, the split array contains all elements from start to the end of the array. If this parameter is negative, it specifies the elements starting from the end of the array.

Return: Returns a new array containing the elements in arrayObject from start to end (excluding this element).

Note: Please note that this method does not modify the array, but returns a subarray. If you want to delete a segment of elements from an array, you should use the method Array.splice().

Note: You can use negative values ​​to select elements from the tail of the array. If end is not specified, the slice() method selects all elements from start to the end of the array.

javascript slice() methodexample

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

<script type="text/javascript">

    var arr = new Array(3)
    arr[0] = "George"
    arr[1] = "John"
    arr[2] = "Thomas"

    document.write(arr + "<br />")
    document.write(arr.slice(1) + "<br />")
    document.write(arr)

</script>

</body>
</html>

Run instance »

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

Popular Recommendations

Home

Videos

Q&A