Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Prime Number Check in JavaScript

Check if a number is prime in JavaScript with trial division up to the square root. Lists primes in a range and factors of 60, with explanation and output.

IntermediateJavaScript Loop ProgramsExample 6 of 6
prime-number-check.js
1// Prime number: divisible only by 1 and itself
2// Examples: 2, 3, 5, 7, 11, 13, 17, 19
3
4// Method 1: Basic check
5function isPrime(n) {
6 if (n < 2) return false;
7 if (n === 2) return true;
8 if (n % 2 === 0) return false;
9
10 for (let i = 3; i < n; i += 2) {
11 if (n % i === 0) return false;
12 }
13 return true;
14}
15
16console.log("2:", isPrime(2));
17console.log("4:", isPrime(4));
18console.log("17:", isPrime(17));
19console.log("20:", isPrime(20));
20
21// Method 2: Optimized (check up to sqrt(n))
22function isPrimeOptimized(n) {
23 if (n < 2) return false;
24 if (n === 2) return true;
25 if (n % 2 === 0) return false;
26
27 let sqrt = Math.sqrt(n);
28 for (let i = 3; i <= sqrt; i += 2) {
29 if (n % i === 0) return false;
30 }
31 return true;
32}
33
34console.log("\nOptimized:");
35console.log("29:", isPrimeOptimized(29));
36console.log("100:", isPrimeOptimized(100));
37
38// Method 3: Find primes in range
39function findPrimesInRange(start, end) {
40 let primes = [];
41 for (let i = start; i <= end; i++) {
42 if (isPrimeOptimized(i)) {
43 primes.push(i);
44 }
45 }
46 return primes;
47}
48
49console.log("\nPrimes between 10 and 30:");
50console.log(findPrimesInRange(10, 30));
51
52// Method 4: Count prime factors
53function countPrimeFactors(n) {
54 let count = 0;
55 let factors = [];
56
57 for (let i = 2; i <= n; i++) {
58 while (n % i === 0) {
59 count++;
60 factors.push(i);
61 n /= i;
62 }
63 }
64
65 return { count, factors };
66}
67
68console.log("\nPrime factors of 60:");
69console.log(countPrimeFactors(60));

Sample Output

2: true
4: false
17: true
20: false

Optimized:
29: true
100: false

Primes between 10 and 30:
[ 11, 13, 17, 19, 23, 29 ]

Prime factors of 60:
{ count: 4, factors: [ 2, 2, 3, 5 ] }

In this one

  • The definition of a prime number
  • Why 1 is not prime and 2 is
  • Why checking up to the square root is enough
  • How to list primes and find prime factors

How it works

  1. 1

    isPrime(2) returns true from the special case.

  2. 2

    isPrime(4) is even, so false.

  3. 3

    isPrime(17) finds no divisor, so true.

  4. 4

    isPrimeOptimized checks 29 up to about 5.4.

  5. 5

    findPrimesInRange collects primes from 10 to 30.

  6. 6

    countPrimeFactors(60) returns 2, 2, 3, 5.

Edge cases

1 and 0

isPrime(1) and isPrime(0) return false.

The n < 2 check handles them.

Forgetting the case for 2

Without it, 2 % 2 === 0 makes 2 look composite.

Check n === 2 before rejecting even numbers.

Perfect squares

For 49, the loop must reach 7.

Use i <= sqrt, not i < sqrt, so the square root itself is tested.

What's going on

A prime number has exactly two divisors: 1 and itself. 2, 3, 5, 7 and 11 are primes. 9 is not, because 3 divides it. 1 is not prime either. People sometimes argue, but math settled it: 1 has only one divisor.

Method 1 does a basic check. Numbers below 2 are not prime. 2 is prime and is the only even prime, so it needs its own line. If you forget that special case, the even check would call 2 composite. Every other even number is rejected right away. Then the loop tries odd divisors 3, 5, 7 and so on. If any divides n with remainder 0, n is not prime.

Method 1 loops up to n - 1, which wastes time. Method 2 stops at the square root. Here is why. If n = a × b, one of a or b must be at most the square root of n. So if nothing up to the square root divides n, nothing larger will either. For 101 that means checking up to about 10, not 100.

Method 3 uses the fast check inside a loop to list primes between 10 and 30.

Method 4 finds prime factors. It divides n by 2 as long as it can, then by 3, and so on. 60 becomes 2 × 2 × 3 × 5. count tells you how many factors there are.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Is 1 a prime number?

Pick an answer to check it.

Question 2

To test 121, what is the largest divisor you need to try?

Pick an answer to check it.

Questions people ask

Why skip even divisors?

If n is odd, no even number can divide it. Skipping them halves the work.

Is the square root check always correct?

Yes. Any factor pair has one member at or below the square root.

How do I find many primes fast?

Use the Sieve of Eratosthenes, which marks multiples in an array.