Here are some JavaScript programs that check if a given number is a prime number.
A prime number is a number greater than 1 that has no positive divisors other than 1 and itself.
Example 1: Basic Prime Number Check
This simple program checks if a number is prime using a for loop.
function isPrime(num) { if (num <= 1) { return false; // Numbers less than 2 are not prime } for (let i = 2; i < num; i++) { if (num % i === 0) { return false; // If divisible by any number other than 1 and itself, not prime } } return true; // Number is prime } // Test the function const number = 29; if (isPrime(number)) { console.log(`${number} is a prime number.`); } else { console.log(`${number} is not a prime number.`); }
Output:
29 is a prime number.
Explanation
The function isPrime(num) checks if the given number num is prime.
If num is less than or equal to 1, it is not a prime number.
The for loop iterates from 2 to num – 1 to check for divisors. If any divisor is found, it returns false.
If no divisors are found, the function returns true.
Example 2: Optimized Prime Number Check
A more efficient way to check if a number is prime is to iterate only up to the square root of the number. This reduces the number of iterations and improves performance.
function isPrime(num) { if (num <= 1) { return false; // Numbers less than 2 are not prime } // Check from 2 to the square root of num for (let i = 2; i <= Math.sqrt(num); i++) { if (num % i === 0) { return false; // If divisible, not prime } } return true; // Number is prime } // Test the function const number = 29; if (isPrime(number)) { console.log(`${number} is a prime number.`); } else { console.log(`${number} is not a prime number.`); }
Output:
29 is a prime number.
Explanation
This optimized version checks divisors only up to the square root of num, significantly reducing the number of iterations.
Example 3: Check Prime Numbers in a Range
This program checks all numbers within a specified range and logs whether each number is prime.
function isPrime(num) { if (num <= 1) { return false; } for (let i = 2; i <= Math.sqrt(num); i++) { if (num % i === 0) { return false; } } return true; } function checkPrimesInRange(start, end) { for (let i = start; i <= end; i++) { if (isPrime(i)) { console.log(`${i} is a prime number.`); } else { console.log(`${i} is not a prime number.`); } } } // Test the function checkPrimesInRange(10, 20);
Output:
10 is not a prime number. 11 is a prime number. 12 is not a prime number. 13 is a prime number. 14 is not a prime number. 15 is not a prime number. 16 is not a prime number. 17 is a prime number. 18 is not a prime number. 19 is a prime number. 20 is not a prime number.
Explanation
The isPrime(num) function is used to check if each number in the range is prime.
The checkPrimesInRange(start, end) function iterates through the specified range and uses isPrime to log whether each number is prime.
Example 4: One-Liner Prime Number Check Using Array.every()
Here’s a one-liner function using the Array.every() method to check if a number is prime.
const isPrime = (num) => num > 1 && [...Array(Math.floor(Math.sqrt(num)) - 1).keys()].map(i => i + 2).every(i => num % i !== 0); // Test the function const number = 29; console.log(isPrime(number) ? `${number} is a prime number.` : `${number} is not a prime number.`);
Output:
29 is a prime number.
Explanation
This one-liner creates an array of numbers from 2 to the square root of num.
The every() method checks that num is not divisible by any of these numbers.
It returns true if num is prime and false otherwise.
These examples show different ways to check if a number is prime in JavaScript, ranging from simple loops to optimized methods. Feel free to use or adapt these examples depending on your use case!