How to create an array of integers in JavaScript?


To create an array of integers in JavaScript, try the following −

var rank = [1, 2, 3, 4];

You can also use the new keyword to create array of integers in JavaScript −

var rank = new Array(1, 2, 3, 4);

The Array parameter is a list of strings or integers. When you specify a single numeric parameter with the Array constructor, you specify the initial length of the array. The maximum length allowed for an array is 4,294,967,295.

Example

Let’s see an example to create an array of integers in JavaScript −

Live Demo

<html>    
   <body>  
      <script>
         var arr1 = [50,60,65,90];
         var arr2 = [25,35,50,90];  
         for (i = 0; i < arr1.length; i++) {  
            for (z = 0; z < arr1.length; z++) {
               if (arr1[i] === arr2[z]) {
                  document.write("<br>Matched element: "+arr2[z]);
               }  
            }  
         }  
      </script>
   </body>
</html>

Output

Matched element: 50
Matched element: 90

Updated on: 19-Jun-2020

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements