JavaScript

# How to check if a number is prime or not in JavaScript

One of the many basic mathematical functions that you can perform using JavaScript is a function that helps you determine whether an integer is prime or not. A prime number can be described as a number that we can only divide fully, by the number 1 and itself. For example, the integer 7 is only capable of dividing by the number 1 and also the number 7.

In this post, you will learn how to implement this concept into JavaScript code.

## How to find whether an integer is prime or not?

The simplest way to check if a number is prime or not involves the use of multiple if/else statements as well as a “for loop”. Let us take a look at the code that will solve this problem:

This code takes a number, performs functions on it, checks whether it is prime or not, and then displays the result. The code has been divided into 3 sections so that you can understand it with more ease.

### Section 1

This section contains variable declaration:

// Section 1

const sampleInt = 7;

var primeBool = true;

var i;

First, we stored the number(to be checked whether it is a prime number or not) into a variable “sampleInt” and declared two other variables(“primeBool” and “i”) for further use.

### Section 2

This section checks whether it is prime or not:

// Section 2
if (sampleInt >1) {
for (i = 2; i < sampleInt; i++) {
if (sampleInt % i == 0) {
primeBool = false;
break;
}
}
}

This is the core section. Here it starts off with a simple if statement to make sure that your integer is greater than one. Afterward, a for loop is used which starts off with an initial value of 2 and increments all the way to n-1(“n” in this case being the integer). Another if statement is used to check if the integer is divisible by any number between the range of 2 and n-1. If it is, the “primeBool” will be assigned the value false.

### Section 3

This section displays the result:

// Section 3
if (primeBool) {
console.log(`\${sampleInt} is a prime number`);
} else {
console.log(`\${sampleInt} is a not prime number`);
}

Once it is checked, whether the provided number is a prime number or not, this section will use if/else statements to print out the respective statement.

As the provided integer is 7, the “prime” value will stay true and this section will print out the following:

7 is a prime number.

This display will give us the answer to the question of whether the entered integer is a prime number or not.

A sample snippet of the code is shown below:

Through these 3 simple stages, the result has been found for the question of whether a certain integer is prime or not?

## Conclusion

To verify whether a number is prime or not, a “for loop” is used to iterate between 2 and n-1, and then our integer is divided by each of those iterated values. If the division is successful (the modulus is 0), it shows that the integer is not prime, otherwise, it is prime. The various sections in this article elaborated in detail on how everything was implemented.

### About the author

#### Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.