JavaScript Prime Number Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is a Prime Number?

A prime number is an integer greater than 1 that has exactly two positive divisors: 1 and itself.

Remember
Prime:     2, 3, 5, 7, 11, 13, 17, 19, ...
Not prime: 1 (neither), 4, 6, 8, 9, 10, 12, ...

Rule:
  n > 1 and no divisor in 2 .. floor(√n)
nDivisorsPrime?
71, 7Yes
91, 3, 9No
21, 2Yes (only even prime)
11No

How to Check a Prime Number in JavaScript

Four clear steps — then reuse the helper to list primes in a range.

  1. If n ≤ 1, return false.
  2. If n is 2 or 3, return true (optional fast path).
  3. Try divisors from 2 to √n; if any divides n, it is not prime.
  4. If no divisor is found, n is prime.
MethodIdeaTimeBest for
Trial to √nNo divisor in 2..√nO(√n)Single check (default)
Skip even divisorsHandle 2, then odd i onlyO(√n)Slightly faster checks
Range scanCall isPrime for each valueO(N√N)Listing primes 1..N

Pseudocode

Pseudocode
function IsPrime(n):
    if n <= 1: return false

    i = 2
    while i * i <= n:
        if n % i == 0:
            return false
        i = i + 1

    return true

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Definitionn > 1 with only divisors 1 and n
1Neither prime nor composite
2Smallest and only even prime
Loop boundi * i <= n (same as i <= √n)
CompositeHas a factor ≤ √n
Classic checks7 yes; 9 no; 1 no

Live Preview

Enter an integer to see trial division up to √n and the prime verdict.

Whole numbers only. Tap a chip or type a value.

Result
n = 17
Check divisors from 2 to floor(√17)=4
No divisor found
17 is prime

JavaScript Programs

Start with a reusable isPrime helper, list primes in a range, then skip even divisors for a small speedup. Each example has a Try it Yourself editor.

Example 1 — isPrime Check

Return true only when no divisor exists from 2 through √n.

JavaScript
function isPrime(n) {
  if (n <= 1) {
    return false;
  }

  for (let i = 2; i * i <= n; i++) {
    if (n % i === 0) {
      return false;
    }
  }

  return true;
}

const number = 17;
if (isPrime(number)) {
  console.log(number + " is prime");
} else {
  console.log(number + " is not prime");
}
Try It Yourself

How It Works

  1. n <= 1 is rejected — primes start at 2.
  2. The loop runs while i * i <= n, which means i ≤ √n.
  3. If any i divides n, a third divisor exists → not prime.
  4. If the loop finishes with no hit, only divisors are 1 and n.
  5. For 17, checks stop at 4 and none divide, so the answer is true.

Example 2 — Primes in a Range

Reuse isPrime to print every prime from 1 to 20.

JavaScript
function isPrime(n) {
  if (n <= 1) {
    return false;
  }

  for (let i = 2; i * i <= n; i++) {
    if (n % i === 0) {
      return false;
    }
  }

  return true;
}

const start = 1;
const end = 20;

console.log("Primes from " + start + " to " + end + ":");
let line = "";
for (let i = start; i <= end; i++) {
  if (isPrime(i)) {
    line += i + " ";
  }
}
console.log(line.trim());
Try It Yourself

How It Works

  1. Same isPrime helper as Example 1 keeps the logic in one place.
  2. The outer loop visits every integer from start to end.
  3. Only values that pass isPrime are collected.
  4. 1 is skipped automatically by the helper.
  5. For larger ranges, a sieve is faster — but this pattern is perfect for beginners.

Example 3 — Skip Even Divisors

Handle 2 specially, then test only odd candidates up to √n.

JavaScript
function isPrime(n) {
  if (n <= 1) {
    return false;
  }
  if (n === 2) {
    return true;
  }
  if (n % 2 === 0) {
    return false;
  }

  for (let i = 3; i * i <= n; i += 2) {
    if (n % i === 0) {
      return false;
    }
  }

  return true;
}

console.log(isPrime(2));  // true
console.log(isPrime(17)); // true
console.log(isPrime(18)); // false
console.log(isPrime(1));  // false
Try It Yourself

How It Works

  1. Reject n <= 1 the same way as Example 1.
  2. Return true for 2, then reject every other even number.
  3. Odd loop starts at 3 and steps by 2 — no even divisors are tested.
  4. Still stops at √n with i * i <= n.
  5. Same answers as Example 1, with roughly half the divisor checks for odd n.

Edge Cases & Pitfalls

Check these before calling the solution done.

1

Not prime

Return false for n ≤ 1 before looping.

2

Even but prime

Do not reject all even numbers — 2 is prime.

Bound

Use i * i ≤ n

Prefer i * i <= n over floating Math.sqrt in the loop condition.

Negatives

Not prime here

This tutorial defines primes on positive integers only.

Time and Space Complexity

ProgramTimeExtra space
isPrime (√n trial)O(√n)O(1)
Skip even divisorsO(√n)O(1)
Primes in range 1..NO(N√N)O(1)

For large ranges, the Sieve of Eratosthenes is faster. For one number or a short range, √n trial division is enough.

Key Takeaways

  • Definition: n > 1 with only divisors 1 and n.
  • Check: no divisor from 2 to √n.
  • Facts: 1 is not prime; 2 is the only even prime.
  • Test set: 17 → yes; 15 → no; 1 → no; 2 → yes.

One line: if n > 1 and nothing from 2 to √n divides it, n is prime.

Frequently Asked Questions

A prime number is an integer greater than 1 with exactly two positive divisors: 1 and itself. Examples: 2, 3, 5, 7, 11.
No. 1 has only one positive divisor, so it is neither prime nor composite.
If n has a factor larger than √n, the matching factor is already smaller than √n and would have been found earlier.
Yes. 2 is the only even prime.
A single isPrime check with trial division up to √n is O(√n). Listing primes from 1 to N by calling isPrime on each value is about O(N√N).
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit the input and Run.

Next: Pronic Number

Check whether a number equals k × (k + 1).

Pronic number tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful