isPrime() Javascript : Prime number program in Javascript

isPrime() Javascript represents a prime number checker function in Javascript.

Prime number is a number which is divisible by one and number itself.

like numbers 5,7,11,13,17, etc are prime numbers.

Prime number program in javascript using for loop

Below program uses for loop to check a number is prime or not.

Create a function isPrime() to check the number is prime or not.

  1. get the number
  2. check a number is divisible by 2 to num/2
  3. if divisible then not a prime number
  4. else it is a prime number.

In above program we have create isPrime() Javascript function.

Prime number program in JavaScript

Function is checking whether a number is prime or not.

To get text box value document.getElementById("num").value is used here document represents the html page and method getElementById is accepting a html elements id.

based on it is finding its value.

To show result in paragraph element with id result document.getElementById("result").innerHTML is used.

Read More

  1. Get multiple checkbox value in javascript
  2. Add active class onclick
  3. JavaScript Multiplication Table
  4. Form Handling and Validation
  5. Try Catch and Finally in JavaScript
  6. Math Object in JavaScript