Numbers · beginner
JavaScript Program to Check Prime Number
Check whether a number is prime in JavaScript by testing divisors up to its square root, step by step, plus the bugs that make 1 or 9 look prime.
- functions
- for loop
- remainder %
- Math.sqrt()
- early return
A prime number is a whole number greater than 1 whose only divisors are 1 and itself: 2, 3, 5, 7, 11 and so on. To test n, look for a divisor between 2 and n - 1; if there is one, n is not prime.
Divisors only need checking up to the square root of n: if n = a × b and both were bigger than √n, their product would be bigger than n. The loop condition d * d <= n expresses that without calling Math.sqrt().
The program
function isPrime(n) {
if (!Number.isInteger(n) || n < 2) return false;
for (let d = 2; d * d <= n; d++) {
if (n % d === 0) return false;
}
return true;
}
for (const n of [1, 2, 9, 29, 97]) {
console.log(n, isPrime(n) ? "is prime" : "is not prime");
}1 is not prime
2 is prime
9 is not prime
29 is prime
97 is primeHow it works
- Numbers below 2, and values that are not whole numbers, are not prime, so the function returns false straight away.
- The loop tries d = 2, 3, 4, … while d * d <= n, which is the same as d <= √n but stays in exact integer arithmetic.
- If n % d is 0, d divides n, so n is not prime and the function returns false immediately.
- If the loop ends without finding a divisor, n is prime. For 2 and 3 the loop body never runs.
Watch it run line by line
Follow isPrime(9): the loop test runs with d = 2, then d = 3, and 9 % 3 === 0 returns false. For 97 the loop runs up to d = 9, and the return row shows true.
“Visualize step by step” opens the program in the JavaScript Compiler and records every step in your browser. Use the arrow keys to move forwards and backwards; changed variables are highlighted.
Another way: All primes below 30 with filter()
Array.from({ length: 30 }, (_, i) => i) builds [0, 1, …, 29]; filter() keeps the numbers isPrime accepts. every() is the array version of "no divisor found".
const isPrime = (n) =>
n > 1 && Array.from({ length: Math.floor(Math.sqrt(n)) - 1 }, (_, i) => i + 2).every((d) => n % d !== 0);
const primes = Array.from({ length: 30 }, (_, i) => i).filter(isPrime);
console.log(primes.join(", "));2, 3, 5, 7, 11, 13, 17, 19, 23, 29Common mistakes
- Forgetting the n < 2 check, so 1 and 0 are reported as prime.
- Looping with d * d < n instead of <=, which misses square numbers: 9, 25 and 49 then look prime.
- Printing "prime" inside the loop as soon as one divisor fails, before all divisors have been checked.
- Testing very large numbers: above Number.MAX_SAFE_INTEGER (about 9 × 10¹⁵) integers lose precision. Use BigInt for those.
Try it yourself
- Count the primes below 10,000 (there are 1,229).
- Return the smallest divisor and print "91 is not prime (7 × 13)".
- Skip even divisors after 2 to halve the work.
Open the program in the JavaScript Compiler, change it and press Run: there is nothing to install, and your code never leaves your browser.
Related examples
Browse all 25 JavaScript examples, or write your own program in the online JavaScript Compiler.