How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Find Prime Numbers in JavaScript (1 to 100) — Fast & Simple Methods
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Find Prime Numbers in JavaScript (1 to 100) — Fast & Simple Methods
Web Development

How to Find Prime Numbers in JavaScript (1 to 100) — Fast & Simple Methods

how7o
By how7o
Last updated: February 6, 2026
5 Min Read
Find prime numbers in JavaScript thumbnail
SHARE

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.

Contents
  • What is a prime number?
  • The key idea: use the modulus operator (%)
  • Step-by-step: Find prime numbers in JavaScript from 1 to 100
    • Step 1: Create a fast isPrime() function (recommended)
    • Step 2: Print prime numbers from 1 to 100
  • A simpler prime checker (easy to understand, but slower)
  • Fast version without Math.sqrt(): i * i <= num
  • Troubleshooting
    • Why does 1 show as prime?
    • Why does everything look prime?
    • Why is it slow for large numbers?
  • Official references
  • Related guides

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

  • MDN: Remainder operator (%)
  • MDN: Math.sqrt()

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.

TAGGED:algorithmsBeginnerscoding interviewJavaScriptloopsmathmodulus operatornumber theoryprime numbers

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Dynamically set site title and tagline in WordPress by country How to Dynamically Set Site Title and Tagline in WordPress (By Country)
Next Article Install MySQL on Ubuntu 22.04 — terminal with apt command and database cylinder icon How to Install MySQL on Ubuntu 22.04: Step-by-Step Guide
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026
Caddy server — automatic HTTPS, 3-line Caddyfile vs 25-line nginx config
How to Install Caddy Server on Ubuntu (Automatic HTTPS, Drop-in nginx Alternative)
May 24, 2026
Cloudflare Tunnel — outbound-only connection from server, no inbound port forward
How to Install Cloudflare Tunnel on Ubuntu (Expose Local Services, No Port Forwarding)
May 24, 2026
WireGuard encrypted tunnel between server and clients with lock icons
How to Set Up WireGuard VPN on Ubuntu (Server, Linux Client, and iOS)
May 24, 2026

You Might Also Like

WordPress get current category ID — three methods by page context
Web Development

How to Get the Current Category ID in WordPress

7 Min Read
Set A4 paper size in CSS for printing
Web Development

How to Set A4 Paper Size in CSS for Print

5 Min Read
Select the last child element with jQuery
Web Development

How to Select the Last Child Element in jQuery

4 Min Read
Enforce a minimum phone-number length in WooCommerce checkout
Web Development

How to Set a Minimum Length for Phone Numbers in WooCommerce

6 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?