I recently needed to find prime numbers in JavaScript from 1 to 100 for a quick script, and I learned the hard way that guessing conditions doesn’t work for primes. Once you use the right rule (divisibility), the solution becomes simple and reusable.
If you tried something like i / i + 1 == 1, don’t worry—prime checking isn’t based on that kind of division trick. The correct way is to test whether the number can be divided evenly by any smaller number (other than 1).
What is a prime number?
A prime number is a number greater than 1 with exactly two divisors:
- 1
- itself
Examples:
- 2 is prime (divisors: 1, 2)
- 3 is prime (divisors: 1, 3)
- 4 is not prime (divisors: 1, 2, 4)
- 1 is not prime (prime numbers must be greater than 1)
The key idea: use the modulus operator (%)
To find prime numbers in JavaScript, you need one reliable check: if a number divides evenly by something other than 1 and itself, it’s not prime.
That’s exactly what the remainder (modulus) operator does:
// If num % i === 0, then i divides num with no remainder
// That means num is NOT prime (for any i between 2 and num-1)
Step-by-step: Find prime numbers in JavaScript from 1 to 100
This approach is clean and scalable:
- Write an
isPrime()function. - Loop from 1 to 100.
- Print only the numbers that are prime.
Step 1: Create a fast isPrime() function (recommended)
Here’s the version I use most often. It checks divisors only up to the square root of the number, which is much faster than checking all the way up to the number itself.
function isPrime(num) {
// Prime numbers must be greater than 1
if (num <= 1) return false;
// Only check divisors up to sqrt(num)
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
Step 2: Print prime numbers from 1 to 100
Now loop through the range and print primes.
for (let i = 1; i <= 100; i++) {
if (isPrime(i)) {
console.log(i + " is a prime number.");
}
}
If you want only the prime numbers (clean output), print just the number:
for (let i = 1; i <= 100; i++) {
if (isPrime(i)) console.log(i);
}
A simpler prime checker (easy to understand, but slower)
If you’re just learning and want the simplest logic, this one checks all numbers from 2 up to num - 1. It works, but it’s slower for big numbers.
function isPrimeSimple(num) {
if (num <= 1) return false;
for (let i = 2; i < num; i++) {
if (num % i === 0) return false;
}
return true;
}
For 1 to 100 it’s totally fine. But if you later test 1 to 100000, use the optimized version.
Fast version without Math.sqrt(): i * i <= num
If you prefer avoiding Math.sqrt(), use this common pattern. It’s fast and clean.
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) return false;
}
return true;
}
Troubleshooting
Why does 1 show as prime?
1 is not a prime number. Make sure you have this check:
if (num <= 1) return false;
Why does everything look prime?
You’re probably missing the divisibility check. This line is the heart of the solution:
if (num % i === 0) return false;
Why is it slow for large numbers?
Don’t loop until num. Use an optimized loop limit like i <= Math.sqrt(num) or i * i <= num.
Official references
Related guides
- JavaScript modulus operator explained
- How to loop through numbers in JavaScript
- How to check even or odd numbers in JavaScript
That’s it. With this isPrime() function, you can find prime numbers in JavaScript for 1 to 100 (or any range) without messy conditions inside your main loop.
