JavaScript program to check prime numbers

In this article, you will learn and get code to check whether the number entered by the user is a prime number or not in JavaScript. Here is the list of programs you will go through:

What is a prime number?

A prime number is a number that can only be divided exactly by 1 and the number itself. For example, 2, 3, 5, 7, 13, 17, etc.

As you can clearly see, the number 17 can only be divided by 1 and 17. Similarly, 13 can only be divided by 1 and 13.

Check Prime Number in JavaScript

Here is the simplest JavaScript program to check whether a number is a prime number or not. This program does not take input from the user.

<!doctype html>
<html>
<body>
<script>
var num, i, chk=0;
num=19;
for(i=2; i<num; i++)
{
  if(num%2==0)
  {
    chk++;
    break;
  }
}
if(chk==0)
  document.write(num + " is a Prime Number");
else
  document.write(num + " is not a Prime Number");
</script>
</body>
</html>

Save this code in a file with .html extension. Open the file in a web browser. Here is the output produced:

javascript check prime number

Now change the value of num to 20. That is, initialize 20 to num. Save the code and open it again, or refresh the browser. Here is the output you will see:

check prime number javascript

Note: The document.write() method writes data into an HTML output.

Get Number Input from the User

Here is another program that asks the user to enter a number to check and print whether the number entered is a prime number or not.

<!doctype html>
<html>
<head>
<script>
var num, i, chk, temp;
function checkPrime()
{
  num = parseInt(document.getElementById("num").value);
  if(num)
  {
    chk=0;
    temp = document.getElementById("resPara");
    temp.style.display = "block";
    for(i=2; i<num; i++)
    {
      if(num%2==0)
      {
        chk++;
        break;
      }
    }
    if(chk==0)
      document.getElementById("res").innerHTML = "a Prime";
    else
      document.getElementById("res").innerHTML = "not a Prime";
  }
}
</script>
</head>
<body>

<p>Enter the Number: <input id="num"><button onclick="checkPrime()">Check</button></p>
<p id="resPara" style="display:none;">It is <span id="res"></span> Number</p>

</body>
</html>

Here is its sample output:

check prime number with user input javascript

Now supply any number, say 37, and click on the "Check" button to check and print a message that tells whether it is a prime number or not, as shown in the snapshot given below:

check prime number or not javascript

The following code:

style="display:none;"

is a CSS code that hides an HTML element where it is present. Because it is present in a p (paragraph) tag whose id is resPara, this paragraph gets hidden initially.

When the user clicks on the button "Check," a function named checkPrime() gets called. The following JavaScript code:

num = parseInt(document.getElementById("num").value);

states that an int (integer) value of an HTML element whose id is num gets initialized to num variable. If num holds any value (instead of nothing or empty), the condition of if, num evaluates to be true, therefore program flow goes inside the loop.

The following JavaScript statement:

temp.style.display = "block";

states that an HTML element whose id is stored in the temp variable gets visible after executing this code. And the following code:

document.getElementById("res").innerHTML = "a Prime";

states that the string value a Prime gets written to an HTML element whose id is res

Live Output of the Previous Program

Here is the live output of the previous JavaScript program that asked the user to enter a number and checked whether it was a prime number or not.

Enter the Number:

JavaScript Online Test


« Previous Program Next Program »


Liked this post? Share it!