How to find prime numbers between 0 – 100 with JavaScript?

Sometimes, we want to find prime numbers between 0 – 100 with JavaScript.

In this article, we’ll look at how to find prime numbers between 0 – 100 with JavaScript.

How to find prime numbers between 0 – 100 with JavaScript?

To find prime numbers between 0 – 100 with JavaScript, we use a for loop.

For instance, we write

const isPrime = (n) => {
  if (n < 2) {
    return false;
  }

  const q = Math.floor(Math.sqrt(n));

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

  return true;
};

to define the isPrime function.

In it, we return false if n is less than 2.

Otherwise, we loop from 2 to the floor of the square root of n.

And we check if each value i is evenly divides n.

If that’s the case, we return false.

Otherwise, we return true.

Conclusion

To find prime numbers between 0 – 100 with JavaScript, we use a for loop.