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
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
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

You Might Also Like

WordPress cron job without a plugin — cron_schedules, wp_schedule_event, and action callback
Web Development

How to Schedule a Cron Job in WordPress Without a Plugin

7 Min Read
Create a Directory in Ubuntu
Server Management

Create a Directory in Ubuntu (mkdir Command + Examples)

4 Min Read
Trigger a function when DataTables loads data via Ajax
Web Development

How to Trigger a Function When DataTables Loads Data

5 Min Read
jQuery add required attribute to input fields — .prop() method
Web Development

How to Add the Required Attribute to Input Fields with jQuery

5 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?