generate range of numbers javascript code example

Example 1: javascript get random number in range

function getRandomNumberBetween(min,max){
    return Math.floor(Math.random()*(max-min+1)+min);
}

//usage example: getRandomNumberBetween(20,400);

Example 2: generate random numbers in js

Math.floor((Math.random() * 100) + 1);
//Generate random numbers between 1 and 100
//Math.random generates [0,1)

Example 3: javascript generate a random integer number in a range of numbers

const min = 1;
const max = 4;
const intNumber = Math.floor(Math.random() * (max - min)) + min;
console.log(intNumber); //> 1, 2, 3

Example 4: range javascript

[...Array(5).keys()];
 => [0, 1, 2, 3, 4]

Example 5: generate array range

for (i of range(1, 5)) {
    console.log(i);
}
/* Output
 * 1 2 3 4 5 */

[...range(1, 5)] // [1, 2, 3, 4, 5]

Example 6: Use Recursion to Create a Range of Numbers

function rangeOfNumbers(startNum, endNum) {
  if (endNum - startNum === 0) {
    return [startNum];
  } else {
    var numbers = rangeOfNumbers(startNum, endNum - 1);
    numbers.push(endNum);
    return numbers;
  }
}