• 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 / JavaScript isPrime – How to Check if a Number is Prime in JavaScript

JavaScript isPrime – How to Check if a Number is Prime in JavaScript

March 17, 2022 Leave a Comment

To tell if a Number in JavaScript is prime, we can use the modulus (remainder) operator % along with an if conditional statement. We can make a simple function that will let us know if a number is prime or not. First, here is the JavaScript code that determines whether a number is prime or not.

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

num in the code above would be the number we want to check is prime or not.

We can put this code into a function so that we only have to enter a number into our function to see if it is prime. The function will return true if the number is prime, and false if the number is not prime.

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;
  }
}

Tell if a Number in JavaScript is Prime with a Click

Below we will provide code to let the user input a number, and then use the our function above to tell if the number is prime or not. Here is our simple HTML setup:

<p>Type a whole number to know if it's prime:</p>
<input id="userVal" type="text" value="">
<div id="click-me" onclick="isPrime()">Get results</div>
<div id="results"></div>

First, we will add an onclick event to the submit button to run a function we will create.

We will then use the value property along with the getElementById method to get the value of the input.

We will then determine if the number is prime using the function we created isPrime() above. We will add to our function above so that we can display the results to the user.

Finally, we will display the results using the textContent property.

function isPrime(){
  
  //Get the user input
  var num = Number(document.getElementById("userVal").value);

  //Check if the number is prime and display the results
  var result = true;
  for (var i = 2; i < num; i++){
    if (num % i == 0){
      // Number is NOT prime
      result = false;
      document.getElementById("results").textContent =  num + " is NOT Prime.";
    }
  }
  if (num > 1 && result != false){
    // Number IS prime
    document.getElementById("results").textContent = num + " is Prime.";
  } else {
    // Number is NOT prime
    document.getElementById("results").textContent =  num + " is NOT Prime.";
  }
}  

The final code and output for this example are below:

Code Output:

Type a whole number to know if it’s prime:

Get results

Full Code:

<p>Type a whole number to know if it's prime:</p>
<input id="userVal" type="text" value="">
<div id="click-me" onclick="isPrime()">Get results</div>
<div id="results"></div>

<script>

function isPrime(){
  var num = Number(document.getElementById("userVal").value);
  var result = true;
  for (var i = 2; i < num; i++){
    if (num % i == 0){
      result = false;
      document.getElementById("results").textContent =  num + " is NOT Prime.";
    }
  }
  if (num > 1 && result != false){
    document.getElementById("results").textContent = num + " is Prime.";
  } else {
    document.getElementById("results").textContent =  num + " is NOT Prime.";
  }
}
</script>

Hopefully this article has been useful in helping you tell if a number in JavaScript isprime.

Other Articles You'll Also Like:

  • 1.  How to Count Vowels in a String Using JavaScript
  • 2.  How to Find the Longest String in an Array in JavaScript
  • 3.  JavaScript Infinite Loop
  • 4.  Using JavaScript to Check if Variable Exists
  • 5.  React Axios Interceptor to Prevent Infinite Loops in JWT Authentication
  • 6.  How to Use JavaScript to Round Up Numbers
  • 7.  How to Remove Non Numbers From String in JavaScript
  • 8.  JavaScript slice – How to Get a Substring From a String
  • 9.  JavaScript Square Root with Math.sqrt() Method
  • 10.  Find Common Elements in Two Arrays 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