Examples
- JavaScript Program to Check if a Number is Positive, Negative, or Zero
- JavaScript Program to Find the Largest Among Three Numbers
- JavaScript Program to Check Prime Number (4 Ways)
- JavaScript Program to Find the Factorial of a Number
- Armstrong Number in JavaScript (6 Programs)
- JavaScript Program to Find HCF or GCD
- JavaScript Program to Find LCM (5 Ways)
- JavaScript Program to Convert Decimal to Binary
JavaScript Program to Check Prime Number (4 Ways)
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11, etc., are prime numbers. Prime numbers are widely used in programming, especially in cryptography and mathematical computations.
In this tutorial, we'll write JavaScript programs to check if a number is prime, using multiple approaches.
Check Prime Number in JavaScript Using for Loop
This is the most straightforward approach to check if a number is prime. We loop through numbers from 2 to the square root of the number and check if it is divisible.
Code
function isPrime(number) {
if (number <= 1) {
return false; // Numbers less than or equal to 1 are not prime
}
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
return false; // Found a divisor other than 1 and itself
}
}
return true; // No divisors found, hence prime
}
// Test the function
const testNumber = 29;
if (isPrime(testNumber)) {
console.log(`${testNumber} is a prime number.`);
} else {
console.log(`${testNumber} is not a prime number.`);
}
Output
29 is a prime number.
Explanation
-
If the number is less than or equal to 1, the function immediately returns false because such numbers are not prime.
-
For numbers greater than 1, the loop checks divisors starting from 2 up to the square root of the number.
-
If any divisor is found, the function returns false.
-
If no divisor is found, the function concludes that the number is prime and returns true.
Prime Number in JavaScript Using Optimized Approach
This approach skips even numbers after checking 2 to reduce unnecessary computations.
Code
function isPrimeOptimized(number) {
if (number <= 1) return false;
if (number === 2) return true; // 2 is the only even prime number
if (number % 2 === 0) return false; // Exclude other even numbers
for (let i = 3; i <= Math.sqrt(number); i += 2) {
if (number % i === 0) return false;
}
return true;
}
// Test the function
const testNumber2 = 97;
console.log(`${testNumber2} is ${isPrimeOptimized(testNumber2) ? "a prime" : "not a prime"} number.`);
Output
97 is a prime number.
Explanation
-
This method first checks if the number is 2, as it is the only even prime number.
-
If the number is even but not 2, it immediately returns false.
-
The loop starts from 3 and increments by 2 to skip all even numbers.
-
If no divisor is found within the loop, the number is declared as prime.
JavaScrip Program to Check Prime Number Using Recursion
A recursive function can be used to check for prime numbers by testing divisors.
Code
function isPrimeRecursive(number, divisor = 2) {
if (number <= 1) return false;
if (divisor > Math.sqrt(number)) return true;
if (number % divisor === 0) return false;
return isPrimeRecursive(number, divisor + 1);
}
// Test the function
const testNumber3 = 19;
console.log(`${testNumber3} is ${isPrimeRecursive(testNumber3) ? "a prime" : "not a prime"} number.`);
Output
19 is a prime number.
Explanation
-
The function starts testing divisors from 2.
-
If the divisor exceeds the square root of the number, the function concludes the number is prime and returns true.
-
If a divisor is found, the function returns false.
-
The function calls itself recursively with the next divisor until a conclusion is reached.
Prime Number Program in JavaScript Using Function
In this example, we'll encapsulate the prime-checking logic inside a reusable function.
Code
function checkPrime(number) {
if (number <= 1) {
return `${number} is not a prime number.`;
}
for (let i = 2; i < number; i++) {
if (number % i === 0) {
return `${number} is not a prime number.`;
}
}
return `${number} is a prime number.`;
}
// Example usage
const num = 7;
console.log(checkPrime(num));
Output
7 is a prime number.
Explanation
-
This function is a simple utility to check if a number is prime.
-
It first checks if the number is less than or equal to 1 and immediately returns a result.
-
The for loop iterates through potential divisors from 2 to the number itself.
-
If any divisor is found, the function returns a message indicating the number is not prime.
-
If no divisor is found, the function confirms the number is prime and returns the appropriate message.