Home > Web Front-end > JS Tutorial > How to use slice method

How to use slice method

不言
Release: 2019-01-29 15:23:08
Original
10178 people have browsed it

slice() is a method of all array instances in How to use slice method. It can return selected elements from an existing array. It has two parameters begin and end, both of which are optional. Let’s take a look below. Specific content.

How to use slice method

Let’s take a look at the basic syntax of the slice() method

array.slice( begin [,end] );
Copy after login

begin: An integer used to specify the starting position of the selection ( The index of the first element is 0). You can use negative values ​​to select from the end of the array. If omitted, the default value is "0" and is optional.

end: End the index obtained from zero.

Let’s look at a specific example

The code is as follows

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<html>
   <head>
      <title>How to use slice method Array slice Method</title>
   </head>
   <body>   
      <script type = "text/javascript">
         var arr = ["orange", "mango", "banana", "sugar", "tea"];
         document.write("arr.slice( 1, 2) : " + arr.slice( 1, 2) ); 
         document.write("<br />arr.slice( 1, 3) : " + arr.slice( 1, 3) ); 
      </script>      
   </body>
</html>       
</body>
</html>
Copy after login

The running results are as follows

How to use slice method

This article The article ends here. For more exciting content, you can pay attention to the relevant column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to use slice method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template