A prime number is a number that is divisible by 1 and itself only. First few prime numbers are: 2, 3, 5, 7, 11, 13, 17, …
A JavaScript uses the DOM model to check the input number is prime or not and display its corresponding alert message on the screen.
Examples:
Input : 4 Output : Not Prime Input : 5 Output : Prime
Example 1: This example display the result by using alert function.
<!DOCTYPE html> <html> <head> <title> Check a number is Prime or not using JavaScript </title> <script type="text/javascript"> // Function to check prime number function p() { var n, i, flag = true; // Getting the value form text // field using DOM n = document.myform.n.value; n = parseInt(n) for(i = 2; i <= n - 1; i++) if (n % i == 0) { flag = false; break; } // Check and display alert message if (flag == true) alert(n + " is prime"); else alert(n + " is not prime"); } </script> </head> <body> <center> <h1>GeeksforGeeks</h1> <h4>check number is prime or not</h4> <hr color="Green"> <form name="myform"> Enter the number: <input type="text" name=n value=""> <br><br> <input type="button" value="Check" onClick="p()"> <br> </form> </center> </body> </html> |
Output:
- Before checking the number:

- After checking the number:

Example 2: This example checks a given number is prime or not and display result on the console.
<!DOCTYPE html> <html> <head> <title> Check a number is Prime or not using JavaScript </title> <style> body { border:2px solid black; width:400px; height:150px; text-align:center; } h1 { color:green; } </style> <script type="text/javascript"> // Function to check for prime number function checkPrime() { var n, i, flag = true; // Getting the value form the // text field using DOM n = document.myform.n.value; n = parseInt(n) for (i = 2; i <= n - 1; i++) if (n % i == 0) { flag = false; break; } // Check and display output if (flag == true) console.log(n + " is prime"); else console.log(n + " is not prime"); } </script> </head> <body> <h1>GeeksforGeeks</h1> <h4>check number is prime or not</h4> <form name="myform"> Enter the number: <input type="text" name=n value=""> <input type="button" value="Check" onClick="checkPrime()"> <br> </form> </center> </body> </html> |
Output:
- Before checking the number:

- After checking the number:

Recommended Posts:
- How to check whether the enter key is pressed in a textbox or not using JavaScript / jQuery ?
- How to check if a string is html or not using JavaScript?
- How to check a date is valid or not using JavaScript ?
- How to check input file is empty or not using JavaScript/jQuery ?
- How to check the input date is equal to today's date or not using JavaScript ?
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to Check Mentioned File Exists or not using JavaScript/jQuery ?
- How to check whether the background image is loaded or not using JavaScript ?
- How to check a URL contains a hash or not using JavaScript ?
- What is the !! (not not) operator in JavaScript?
- How to check if the clicked element is a div or not in JavaScript ?
- Check if an array is empty or not in JavaScript
- How to check all values of an array are equal or not in JavaScript ?
- How to solve JavaScript heap out of memory on prime number ?
- PHP | Check if a number is prime
- Detect a device is iOS or not using JavaScript
- How to detect flash is installed or not using JavaScript ?
- How to get the elements of one array which are not present in another array using JavaScript?
- How to validate an input is alphanumeric or not using JavaScript ?
- How to detect the Internet connection is offline or not using JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


