![]() |
VOOZH | about |
There are multiple approaches for creating an array of all integers between two numbers, inclusive, in JavaScript/jQuery. Here are some of the common methods:
The for loop or while loop iterates through all the integers between the two given numbers and pushes them into an array.
Syntax:
for (statement 1 ; statement 2 ; statement 3){
code here...
}Example: In this example, we will see the use of for loop and push().
[ 1, 2, 3, 4, 5 ]
Use Array.from() method to create an array of all integers between two numbers, inclusive.
Syntax:
function createArray(start, end) {
return Array.from({length: end - start + 1},
(_, index) => start + index);
}Example: In this example, we will see the use of Array.from() method.
[ 1, 2, 3, 4, 5 ]
The program starts by defining the start and end of the numeric range as start and end respectively, which in this case are set to 3 and 9. By using a map we iterate between the number and get the value.
Output: