0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Check Prime Number

Use a function like 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; } to check if a number is prime.
📋

Examples

Input2
Outputtrue
Input15
Outputfalse
Input17
Outputtrue
🧠

How to Think About It

To check if a number is prime, first understand that a prime number is greater than 1 and has no divisors other than 1 and itself. So, check if the number is less than or equal to 1, then it is not prime. Otherwise, try dividing the number by all integers starting from 2 up to the square root of the number. If any division results in zero remainder, the number is not prime. If none do, it is prime.
📐

Algorithm

1
Get the input number.
2
If the number is less than or equal to 1, return false (not prime).
3
Loop from 2 to the square root of the number.
4
For each number in the loop, check if it divides the input number evenly.
5
If yes, return false (not prime).
6
If the loop finishes without finding a divisor, return true (prime).
💻

Code

javascript
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;
}

console.log(isPrime(2));  // true
console.log(isPrime(15)); // false
console.log(isPrime(17)); // true
Output
true false true
🔍

Dry Run

Let's trace the number 15 through the code to see if it is prime.

1

Check if number is <= 1

15 is greater than 1, so continue.

2

Start loop from 2 to sqrt(15)

sqrt(15) is about 3.87, so loop i = 2 to 3.

3

Check divisibility by 2

15 % 2 = 1 (not zero), continue.

4

Check divisibility by 3

15 % 3 = 0 (zero), so return false.

inum % iIs num divisible?
21No
30Yes - Not prime
💡

Why This Works

Step 1: Exclude numbers less than or equal to 1

Numbers 1 and below are not prime by definition, so we return false immediately.

Step 2: Loop only up to square root

Checking divisors beyond the square root is unnecessary because any larger factor pairs with a smaller one already checked.

Step 3: Return true if no divisors found

If no number divides evenly, the number is prime, so we return true.

🔄

Alternative Approaches

Check divisibility up to n-1
javascript
function isPrime(num) {
  if (num <= 1) return false;
  for (let i = 2; i < num; i++) {
    if (num % i === 0) return false;
  }
  return true;
}
console.log(isPrime(17));
This method is simpler but slower because it checks more numbers than needed.
Use recursion
javascript
function isPrime(num, i = 2) {
  if (num <= 1) return false;
  if (i > Math.sqrt(num)) return true;
  if (num % i === 0) return false;
  return isPrime(num, i + 1);
}
console.log(isPrime(17));
This recursive method is elegant but can be less efficient and harder to read for beginners.

Complexity: O(√n) time, O(1) space

Time Complexity

The loop runs up to the square root of the input number, so the time grows roughly with √n.

Space Complexity

The program uses a fixed amount of extra space regardless of input size, so space is O(1).

Which Approach is Fastest?

Checking up to the square root is faster than checking all numbers up to n-1. Recursive methods add overhead and are less efficient.

ApproachTimeSpaceBest For
Check up to sqrt(n)O(√n)O(1)Efficient and simple
Check up to n-1O(n)O(1)Simple but slow
Recursive checkO(√n)O(√n)Elegant but uses more space
💡
Only check divisors up to the square root of the number to save time.
⚠️
Checking divisibility up to the number itself instead of up to its square root wastes time.