Forgotten javascript's slice of method

  • 2020-06-01 08:09:40
  • OfStack

The slice() method returns the selected element from an existing array.

Well, I admit I forgot about it!

I'm going to review 1 this time


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 />")


Select the array indexes 2 through 4

Example code:

Example 1:
The code is as follows:

var a="abcdefgmnlxyz";

Intercepts a string from position "2" to position "3", but the character d corresponding to position "3" is not included in the intercept return. Output result :c.

Example 2:
The code is as follows:

var a="abcdefgmnlxyz";

If the second argument is omitted, all characters from position "2" to the end of the string are intercepted. Output result :cdefgmnlxyz.

That's all for this article, I hope you enjoy it.

Related articles: