How to Check Prime Number in JavaScript19 Apr 2025 | 6 min read Introduction to Prime Numbers in JavaScriptA prime number is an integer larger than one that possesses only one and itself as its divisors. Therefore, if a prime number is divided by any integer lower than itself, it will never yield a whole number. A frequent exercise in programming is making a program test if a number is prime or not. This can be implemented on the client side using JavaScript and force the client computer to do the calculation. The following article is going to explain how to test if a number is prime or not using JavaScript. LogicWe can check if a number is prime by dividing the number by all the integers from 1 up to one less than the number. If there is no remainder in any of these divisions, then the number is divisible and, therefore, not prime. Let's take a look at some examples for better comprehension: 13 is only divisible by 1 and 13 itself; if we divide it by any other number, like 7, then the remainder will be 6. But 12 has the divisors 1, 2, 3, 4, and 6, so it is not a prime number. Here, we can observe that the smallest prime number is 2, and surprisingly, it is the only even prime number. Here are the first 20 primes: There are several ways to test if a number is prime, all referred to as primality tests. Methods to Test Prime Numbers in JavaScriptExample 1: Using For Loop The simplest way to check if a number (let's call it x) is a prime is to see if there are divisors (let's call them d) of x with 1 < d < x. Also, we are aware that for any number x, there can never be divisors d such that d > x/2. We will thus look for any number in the range [2, x/2] that will be a divisor of x. If we can obtain one, then we can say that x will not be a prime number, otherwise, it is. Code We called the function checkForPrime() when the value was input. We first verified whether the user had provided valid input. The input cannot be negative or empty since prime numbers cannot be negative. We then used a flag to verify whether, while dividing the number by all the integers from 2 to n-1, we ever obtained a zero remainder. If we ever obtained a zero, it indicates that the number is not prime, so we would reverse the flag's status, terminate the loop, and mark that number as not prime. If the flag's status never gets reversed, it indicates that we never obtained a zero remainder, and we can mark that number as prime. Output ![]() ![]() Example 2: Using While Loop This example here is to showcase another way of implementation in JavaScript. Code Output ![]() ![]() In the present case, a while loop is employed to test for divisibility of the number, restricting the tests to n/2. This approach is founded on a mathematical theorem that says that a number can never be evenly divisible by any integer higher than half of the number. By this approach, we increase efficiency by reducing the number of divisions required. Example 3: Using Recursion Code Output ![]() ![]() Here, a new function based on recursion has been defined. It has base cases in line with the intrinsic logic, making the function call itself until it reaches such base cases. The function will divide the number by each of the divisors from 2 to n-1 and yield a result depending on the remainder. Conclusion
Next TopicJavaScript Pointers |
What is JavaScript shift? In JavaScript, the array shift() method removes the first element of the array and reduces the size of the original array by 1. In JavaScript, shift() is a method that operates on an array and is used to remove the first element from the...
4 min read
When it comes to web development, JavaScript is one of the most popular and adaptable programming languages. Because of its adaptability, it is a fundamental component of contemporary web development, enabling developers to create dynamic and interactive web pages. The ability to extract meaning from code...
9 min read
What is Remix.JS? In JavaScript, Remix JS is a Full Stack React-based framework that helps us to render the code on the server, which tends to result in better performance and SEO as compared to using React on the client side. In simple words, Remix is built to...
6 min read
What is JavaScript? It is a programming language that is used to add interactivity, animation, and dynamic content to websites. or we can say JavaScript is a scripting language that is run on the user's web browser rather than on the web server. JavaScript is also used so...
6 min read
The regex "\n" shows the available new line in the input string in JavaScript. If the new line element is available, then the position of the values shows; otherwise "-1" value shows using the search method. We can test, search, and match the required string...
4 min read
Introduction: The number guessing game is a work of art and engaging programming exercise that assists designers with improving their logical reasoning and critical thinking abilities. In this article, we will dig into the hypothetical parts of making a number guessing game utilizing JavaScript. This famous game...
8 min read
An important asset for anyone wanting to work as a web developer is JavaScript. According to the Developer Survey, JavaScript holds the top spot as the most frequently utilized programming language. In a year, the top JavaScript boot camp schools will teach you all the practical...
14 min read
JavaScript, often simply called 'Javascript' or 'JS', is one of the web languages that includes HTML and CSS. This versatility gives developers the power to create dynamic websites, server-side apps, and mobile or desktop apps. So, if you are an aspiring developer in the tech world,...
6 min read
Overview In Node.js, event emitters are objects that signal the completion of an action by sending a message. JavaScript programmers can write code that listens for events from an event emitter and then triggers functions each time the event is triggered. An identifying string and any data...
5 min read
In JavaScript, a JavaScript engineer is a person who will use JavaScript for creating applications in software. JavaScript engineer is also known as JavaScript engineer. There are some responsibilities to JavaScript engineers including the development of code libraries and optimizing applications for durability and scalability. If...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India