Create Array of Integers Between Two Numbers Inclusive in JavaScript/jQuery Last Updated : 26 Jun, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Using loop Using Array.from() methodUsing jQuery Method 1: Using LoopsThe 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(). JavaScript function createArray(start, end) { let result = []; for (let i = start; i <= end; i++) { result.push(i); } return result; } let result = createArray(1, 5); // Output: [1, 2, 3, 4, 5] console.log(result); Output[ 1, 2, 3, 4, 5 ]Method 2: Using Array.from() methodUse 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. JavaScript function createArray(start, end) { return Array.from({ length: end - start + 1 }, (_, index) => start + index); } let result = createArray(1, 5); // Output: [1, 2, 3, 4, 5] console.log(result); Output[ 1, 2, 3, 4, 5 ]Method 3: jQuery approach using for loopThe 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. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <script src= "http://code.jquery.com/jquery-3.1.0.min.js" integrity= "sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"> </script> </head> <body> <script> // Start and end let start = 3; let end = 9; // Create an array of all integers // between start and end using jQuery let range = $.map(new Array(end - start + 1), function (val, index) { return index + start; }); document.write("[" + range.join(", ") + "]"); </script> </body> </html> Output: Create an array of all integers between two numbers, inclusive, in JavaScript/jQuery Comment More infoAdvertise with us Next Article JavaScript Program to Display Armstrong Number Between Two Intervals L laxmigangarajula03 Follow Improve Article Tags : JQuery javascript-array JavaScript-DSA JavaScript-Questions jQuery-Questions +1 More Similar Reads JavaScript Program to Display Armstrong Number Between Two Intervals An Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. In this article, we'll explore how to create a JavaScript program to display all Armstrong numbers within a 2 min read How to get Almost Increasing Sequence of Integers in JavaScript? JavaScript can be used to generate an almost increasing sequence of integers. An 'almost increasing sequence' is characterized by each element being greater than or equal to its predecessor, except at most one element being smaller than its preceding element. Our objective is to determine the length 3 min read JavaScript - Convert a Number into JS Array You have a number, like 12345, and you need to convert it into an array where each element represents a digit of the number. For example, 12345 should become [1, 2, 3, 4, 5]. How can you achieve this in JavaScript?In JavaScript, there are various ways to transform a number into an array of its digit 3 min read Convert a String to an Integer in JavaScript These are the following ways to convert string to an integer in JavaScript:1. Using Number() MethodThe number() method converts a string into an integer number. It works similarly to the unary plus operator. JavaScriptlet s = "100.45"; console.log(typeof Number(s));Outputnumber 2. Using Unary + Oper 2 min read JavaScript Program to Display Prime Numbers Between Two Intervals Using Functions Prime numbers are natural numbers greater than 1 that are only divisible by 1 and themselves. In this article, we will explore how to create a JavaScript program to display all prime numbers between two given intervals using functions. Approach 1: Using Basic Loop and FunctionThe most straightforwar 2 min read How to Generate a Random Number in JavaScript? To generate a random number in JavaScript, use the built-in methods that produce a floating-point number between 0 (inclusive) and 1 (exclusive).Below are the approaches to generate random numbers in JavaScript:Table of ContentUsing Math.random() methodUsing Math.floor() with Math.random()Using Math 2 min read Like