Skip to content

Get first 10 elements of an array in JavaScript | Example code

  • by

How to get the first 10 elements of an array in JavaScript?

To get the first n elements of an array, use JS slice method.

array.slice(0, 10);

A complete example of a JavaScript Array gets the first 10 items:- Passing the index value in slice method 0 and 10.

Note: Array index start from 0.

 <html>  
<head>  
    <title>Sample Code</title>  
    <script type="text/javascript">  
  	var num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
	alert(num.slice(0, 10))
    </script>  
</head>  
</html> 

Output:

Get first 10 elements of an array in JavaScript

Do comment if you have any questions and suggestions on this topic.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading