JavaScript Program to Check Prime Number By Creating a Function Last Updated : 19 Feb, 2024 Comments Improve Suggest changes Like Article Like Report A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this article, we will explore how to check if a given number is a prime number in JavaScript by creating a function. Table of Content Check Prime Number using for LoopCheck Prime Number using Array MethodsCheck Prime Number using RecursionCheck Prime Number using for LoopThe basic method to check for a prime number is by using a loop to divide the number by each integer less than its square root. JavaScript function isPrime(number) { if (number <= 1) { return false; } for (let i = 2; i <= Math.sqrt(number); i++) { if (number % i === 0) { return false; } } return true; } // Driver code console.log(isPrime(5)); console.log(isPrime(4)); console.log(isPrime(11)); console.log(isPrime(21)); Outputtrue false true falseCheck Prime Number using Array MethodsWe can also use JavaScript array methods to create a more functional approach. In this approach, we create an array of potential divisors using Array.from and then use the every method to check that none of them divides the number evenly. JavaScript function isPrime(number) { return number > 1 && Array.from( { length: Math.sqrt(number) - 1 }, (_, i) => i + 2) .every(divisor => number % divisor !== 0); } console.log(isPrime(5)); console.log(isPrime(4)); console.log(isPrime(11)); console.log(isPrime(21)); Outputtrue false true falseCheck Prime Number using RecursionRecursion can also be used to check for prime numbers. We will create a method which will call itself again and again to check whether the passed number is prime or not. JavaScript function isPrime(number, divisor = 2) { if (number <= 1) { return false; } else if (divisor > Math.sqrt(number)) { return true; } else if (number % divisor === 0) { return false; } else { return isPrime(number, divisor + 1); } } console.log(isPrime(5)); console.log(isPrime(4)); console.log(isPrime(11)); console.log(isPrime(21)); Outputtrue false true false Comment More infoAdvertise with us Next Article JavaScript Program to Check Prime Number By Creating a Function V vkash8574 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads JavaScript Program to Display Prime Numbers Between Two Intervals Using Functions Prime numbers are natural numbers greater than 1 that are only divisible by 1 and themselves. In this article, we will explore how to create a JavaScript program to display all prime numbers between two given intervals using functions. Approach 1: Using Basic Loop and FunctionThe most straightforwar 2 min read JavaScript Program to Print Prime Numbers from 1 to N A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this article, we'll explore how to create a JavaScript program to print all prime numbers from 1 to a given number N. To find prime numbers from 1 to N, we need to: Iterate through all numbers 3 min read JavaScript Application To Check Prime and Non-Prime Number Prime numbers have been a fundamental concept in mathematics and computer science. In programming, checking if a number is prime will help to understand loops, conditions, and optimization techniques.What We Are Going to CreateWe will create a simple web application where users can input a number to 4 min read Check a Number is Prime or Not Using JavaScript A prime number is a whole number greater than 1, which has no positive divisors other than 1 and itself. In other words, prime numbers cannot be formed by multiplying two smaller natural numbers. For example:2, 3, 5, 7, 11, and 13 are prime numbers.4, 6, 8, 9, and 12 are not prime numbers because th 5 min read Javascript Program for Count Primes in Ranges Given a range [L, R], we need to find the count of total numbers of prime numbers in the range [L, R] where 0 <= L <= R < 10000. Consider that there are a large number of queries for different ranges.Examples: Input : Query 1 : L = 1, R = 10 Query 2 : L = 5, R = 10Output : 4 2Explanation :P 3 min read How to Check if the Number is Prime Number in Excel? A prime number is a number that is greater than 1 and has no positive divisor other than 1 and the number itself. Approach: To check whether the number is prime or not we have to divide the number with all the numbers between 2 and the square root of that number. And if it gives remainder 0 in any o 2 min read How to Determine if a Number is Prime in Ruby? In this article, we will discuss how to Determine if a Number is Prime and contains a specific value in ruby. We can determine if a Number is Prime through different methods. Let us study them in detail Table of Content Determine if a Number is Prime using Prime Library ApproachDetermine if a Number 3 min read PHP | Check if a number is prime A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The smallest prime number is 2, which is the only even prime number. All other even numbers are not prime because they are divisible by 2.2 is prime because it can only be divided by 1 and 2.3 is 3 min read What are the methods to find prime numbers? Answer: To find prime numbers, you can use methods like the Sieve of Eratosthenes, trial division, prime factorization, or online tools for quick identification.Here's a more detailed explanation:Sieve of Eratosthenes:Start with a list of numbers up to a certain limit.Mark 2 as prime and eliminate i 2 min read How to Find Prime Number in Golang? Prime numbers are a fascinating concept in the field of mathematics and computer science. They are numbers that have only two distinct positive divisors: 1 and the number itself. This article will guide you through the process of finding prime numbers in Golang, a statically typed, compiled language 6 min read Like