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 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 Like