Skip to content

JavaScript generate all numbers between 2 numbers | Example code

  • by

Using for loop or while Loop in JavaScript you can create an array that contains all integers between start number and end number with including both.

JavaScript generate all numbers between 2 numbers Example

HTML example code: JavaScript program of generate an array between two integers of 1 step length.

For loop: Generate number between 25 to 30

<!DOCTYPE html>
<html> 
<body>

    <script type="text/javascript">
        var lowEnd = 25;
        var highEnd = 30;
        var list = [];
        for (var i = lowEnd; i <= highEnd; i++) {
            list.push(i);
        }
        console.log(list);
    </script>
</body>
</html>

Output:

JavaScript generate all numbers between 2 numbers HTML

While loop: Generate number between 1 to 25

<!DOCTYPE html>
<html> 
<body>

    <script type="text/javascript">
        var lowEnd = 1;
        var highEnd = 25;
        var arr = [];
        while(lowEnd <= highEnd){
         arr.push(lowEnd++);
     }
     console.log(arr);
 </script>
</body>
</html>

Do comment if you have another example or suggestion on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

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