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 |
Observables are a push-based technique for dealing with asynchronous data streams. They were particularly introduced via the Reactive Extensions for JavaScript (RxJS). Observables are more flexible and beneficial in complicated systems where processing numerous asynchronous activities or real-time updates is required, rather than promises, which deal...
8 min read
The javascript regex modifier controls and operates input data with the metacharacters and other regular expressions. The modifier works with the metacharacter and quantifiers to get the particular field. The search(), match(), test(), exec() method works with the regex modifier in javascript. The three modifiers...
7 min read
The alert() method in JavaScript is used to display a virtual alert box. It is mostly used to give a warning message to the users. It displays an alert dialog box that consists of some specified message (which is optional) and an OK button. When...
4 min read
We will study the JavaScript matrix in this article. Let us first comprehend about array and matrix. We all know how to create a dimensional array but most of us do not know how to create a 2-dimensional array so in this article we will learn...
5 min read
We will discuss the in this article. Browser extension A browser extension is used to add the properties of a particular software on the web browser. Web browsers permit you to install different kinds of software extensions and permit you to extend the properties of the browser. In...
3 min read
We all very well know that JavaScript is considered the heart of web development. A webpage is created using three basic languages which are HTML, CSS and JavaScript. The HTML is utilized to give the structure to a webpage. The CSS is utilized to give style to...
8 min read
? Introduction JavaScript is one of the languages for web development. It can perform many activities. Also, there are many methods for sorting, scrolling, selecting, slicing, and finding to work on the entire array. Here in this article, we will check each method by performing and also we...
4 min read
Unit and functional testing is the most useful method to make sure the quality of your JavaScript code. We can choose the best tool for JavaScript testing based on our individual needs, as each tool offers specific advantages to improve testing capabilities. Testing JavaScript code in a...
5 min read
A key component of web development is JavaScript which is well-known for its adaptability and dynamic quality. Understanding the pass by value and pass by reference procedures that are utilized to pass variables across functions is a basic component of programming in JavaScript. Developers may...
6 min read
While both JavaScript and ReactJS are crucial for contemporary web development, their capabilities are incredibly unique. It is fundamental for developers to understand these distinctions to make versatile, dynamic, and viable online apps. This article investigates the key qualifications, applications, and advantages among React.js and JavaScript. What...
11 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