• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Get a List of Prime Numbers Using JavaScript

Get a List of Prime Numbers Using JavaScript

July 20, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Convert Month Number to Name
  • 2.  parseFloat JavaScript – How to use the JavaScript parseFloat() method
  • 3.  Reverse Words in a String JavaScript
  • 4.  Using Javascript to Check if Value is Not Undefined
  • 5.  JavaScript tan – Find Tangent of Number in Radians Using Math.tan()
  • 6.  cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works
  • 7.  Using JavaScript to Square a Number
  • 8.  JavaScript Round to Nearest 10
  • 9.  Using JavaScript to Add Items to Set
  • 10.  Add Days to a Date Using JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy