Prime Number Check | Easily in JavaScript

Prime Number Check

What Prime Number?

Prime Number is a number that is divisible by only 1 and that number. Suppose 13 is a prime number, because 13 is divisible by 1 (13 % 1 == 0) and 13 is also divisible by 13 (13 % 13 == 0)

1. Check a number either it is prime or not

Here is code for isPrime function that will check either a number is prime or not.

JavaScript Code for isPrime function

function isPrime(num) {
  for(var i = 2; i < num; i++)
    if(num % i === 0) return false;
  return num !== 1 && num !== 0;
}
alert(isPrime(2));

2. Print prime number list from a range

JavaScript Code for display prime number list from starting number to ending number using a for loop

function printPrimes(sn, en){
for(sn; sn <= en; sn++){
	if(isPrime(sn)){
	document.write(sn+"<hr/>");
	}
	} 
	}
printPrimes(1,100);

If there is any issue, please write a comment. If this post is helpful , then please share in your social media account. Check out Perfect email validation using JavaScript.

1 thought on “Prime Number Check | Easily in JavaScript

Leave a Reply