Home >Web Front-end >JS Tutorial >The forgotten slice() method of javascript_javascript tips
The slice() method returns selected elements from an existing array.
Okay, I admit I forgot about it!
I’m reviewing it this time
Grammar
arrayObject.slice(start,end)
Array.slice(start, end)
<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.slice(2,4) + "<br />") </script>
Thomas,James
Select array index 2 to 4
Example code:
Example 1:
The code is as follows:
var a="abcdefgmnlxyz"; console.log(a.slice(2,3));
Intercept the string between position "2" and position "3", but the character d corresponding to position "3" is not included in the interception return. Output result: c.
Example 2:
The code is as follows:
var a="abcdefgmnlxyz"; console.log(a.slice(2));
If the second parameter is omitted, all characters from position "2" to the end of the string will be intercepted. Output result: cdefgmnlxyz.
The above is the entire content of this article, I hope you all like it.