# Check if a number is prime in JavaScript

Checking for prime numbers is a common task in mathematics and computer science. It's also pretty common in coding interviews and challenges. Let's see how to check if a number is prime and how to generate prime numbers up to a given number.

## Check if a number is prime

To check if an integer is a prime number, we can simply use a for loop. Inside the loop we can check if the number is divisible by any number from 2 to the square root of the given number. If such a number is found, we can return false. If no such number is found, we can return true, unless the number is less than 2.

const isPrime = num => {
const boundary = Math.floor(Math.sqrt(num));
for (let i = 2; i <= boundary; i++) if (num % i === 0) return false;
return num >= 2;
};

isPrime(11); // true

## Generate primes up to a given number

Using the Sieve of Eratosthenes algorithm, we can generate primes up to a given number. The algorithm works as follows:

• Generate an array from 2 to the given number.
• Use Array.prototype.filter() to filter out the values divisible by any number from 2 to the square root of the provided number.
const primes = num => {
let arr = Array.from({ length: num - 1 }).map((x, i) => i + 2),
sqroot = Math.floor(Math.sqrt(num)),
numsTillSqroot = Array.from({ length: sqroot - 1 }).map((x, i) => i + 2);
numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y === x)));
return arr;
};

primes(10); // [2, 3, 5, 7]

## More like this

• Collection · 25 snippets

### JavaScript Algorithms

Learn a handful of popular algorithms, implemented in JavaScript ES6.

• JavaScript ·

### Find the prime factors of a number in JavaScript

Find the prime factors of a number using trial division in JavaScript.

• JavaScript ·

### How can I check if a number is even or odd using JavaScript?

Check if a number is even or odd using JavaScript using the modulo operator or bitwise AND operator.

• JavaScript ·

### Check if two numbers are approximately equal in JavaScript

Learn how to check if two numbers are approximately equal to each other in JavaScript.

Start typing a keyphrase to see matching snippets.