We can create a list of prime numbers in JavaScript easily – all we need is a custom function to check if a number is prime or not.

We have a post on how to see if a number is prime or not, so we will use the function isPrime from that post to help us create a list of prime numbers.

Here is the function to determine if a number is prime or not:

function isPrime(num) {
  for (var i = 2; i < num; i++){
    if (num % i == 0){
      // Number is NOT prime
      return false;
    }
  }
  if (num > 1){
    // Number IS prime
    return true;
  } else {
    // Number is NOT prime
    return false;
  }
}

To generate a list of the first N prime numbers in JavaScript, we can create our own function and loop until we have N prime numbers.

The function will just take one parameter, the number of prime numbers we want. And it will return an array of the prime numbers. It will use the helper function isPrime().

function isPrime(num) {
  for (var i = 2; i < num; i++){
    if (num % i == 0){
      // Number is NOT prime
      return false;
    }
  }
  if (num > 1){
    // Number IS prime
    return true;
  } else {
    // Number is NOT prime
    return false;
  }
}

function getPrimeNumbers(num) {
  var arr = [];
  var counter = 2;
  while ( arr.length < num ) {
    if( isPrime(counter) ){
      arr.push(counter);
    }
    counter++;
  }
  return arr;
};

So let's just test this quickly with a couple of examples:

function isPrime(num) {
  for (var i = 2; i < num; i++){
    if (num % i == 0){
      // Number is NOT prime
      return false;
    }
  }
  if (num > 1){
    // Number IS prime
    return true;
  } else {
    // Number is NOT prime
    return false;
  }
}

function getPrimeNumbers(num) {
  var arr = [];
  var counter = 2;
  while ( arr.length < num ) {
    if( isPrime(counter) ){
      arr.push(counter);
    }
    counter++;
  }
  return arr;
};

console.log(getPrimeNumbers(0));
console.log(getPrimeNumbers(2));
console.log(getPrimeNumbers(10));
console.log(getPrimeNumbers(20));

#Output
[]
(2) [2, 3]
(10) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
(20) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]

Creating a List of Prime Numbers in a Range Using JavaScript

Another example where we might want to create a list of prime numbers is if we want only the prime numbers in a specific range.

If we want to create a list of prime numbers in a certain range, we can create our own function and loop over that range to find prime numbers.

We will just have to tweak our previous function above slightly. Below is a function that we can use which will get the prime numbers in a range using JavaScript.

function isPrime(num) {
  for (var i = 2; i < num; i++){
    if (num % i == 0){
      // Number is NOT prime
      return false;
    }
  }
  if (num > 1){
    // Number IS prime
    return true;
  } else {
    // Number is NOT prime
    return false;
  }
}

function getPrimeNumbersInRange(num1,num2) {
  var arr = [];
  if( num1 > num2 ) {
    return "Invalid Range";
  } else {
    if ( num1 < 2 ){
      num1 = 2;
    }
    for( var i = num1; i <= num2; i++ ){
      if( isPrime(i) ){
        arr.push(i);
      }
    }
    return arr;
  }
};

Now lets see this in action with some examples:

function isPrime(num) {
  for (var i = 2; i < num; i++){
    if (num % i == 0){
      // Number is NOT prime
      return false;
    }
  }
  if (num > 1){
    // Number IS prime
    return true;
  } else {
    // Number is NOT prime
    return false;
  }
}

function getPrimeNumbersInRange(num1,num2) {
  var arr = [];
  if( num1 > num2 ) {
    return "Invalid Range";
  } else {
    if ( num1 < 2 ){
      num1 = 2;
    }
    for( var i = num1; i <= num2; i++ ){
      if( isPrime(i) ){
        arr.push(i);
      }
    }
    return arr;
  }
};

console.log(getPrimeNumbersInRange(0,10));
console.log(getPrimeNumbersInRange(-100,100));
console.log(getPrimeNumbersInRange(3,3));
console.log(getPrimeNumbersInRange(8,3));
console.log(getPrimeNumbersInRange(100,200));

#Output
(4) [2, 3, 5, 7]
(25) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
[3]
Invalid Range
(21) [101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]

Hopefully this article has been useful for you to learn how to get prime numbers in JavaScript.

Categorized in:

JavaScript,

Last Update: March 12, 2024