How7o
  • Home
  • Marketing
    MarketingShow More
  • OS
    OSShow More
    How to force quit frozen apps in Ubuntu
    Force Close an App in Ubuntu (xkill, System Monitor, kill -9)
    4 Min Read
  • Features
    FeaturesShow More
  • Guide
    GuideShow More
  • Contact
  • Blog
Reading: How to Find Prime Numbers in JavaScript (1 to 100) — Fast & Simple Methods
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Categories
    • Marketing
    • OS
    • Features
    • Guide
    • Lifestyle
    • Wellness
    • Healthy
    • Nutrition
  • More Foxiz
    • Blog Index
    • Complaint
    • Sitemap
    • Advertise
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > 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.
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)
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Find prime numbers in JavaScript thumbnail
How to Find Prime Numbers in JavaScript (1 to 100) — Fast & Simple Methods
February 6, 2026
Dynamically set site title and tagline in WordPress by country
How to Dynamically Set Site Title and Tagline in WordPress (By Country)
February 6, 2026
Capitalize all words in JavaScript with a ucwords-style function
Capitalize All Words in JavaScript (ucwords Equivalent) + First Letter Uppercase
February 6, 2026
CSS page break for printing shown in a print preview layout
CSS Page Break for Printing: How to Split a Web Page Into Multiple Printed Pages
February 6, 2026
get .env variable in Laravel using env and config
How to Get .env Variables in Laravel (Controller + Blade) Safely
February 6, 2026

You Might Also Like

Include Composer packages in plain PHP projects
Web Development

How to Include Composer Packages in Plain PHP Projects (Autoload + Example)

5 Min Read
Display only the current date in Laravel using Carbon
Web Development

How to Display Only the Current Date in Laravel (Carbon Examples)

4 Min Read
WooCommerce homepage filter to hide out of stock products
Web Development

Hide Out of Stock Products from Homepage in WooCommerce (Keep Them Visible Elsewhere)

5 Min Read
Fix 409 Conflict error in Laravel (cookies, cache, WAF)
Web Development

How I Fixed the 409 Conflict Error in Laravel (Cookie / Browser / WAF Fix)

7 Min Read

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!
How7o

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

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?