Navigate back to the homepage

# How to check if number is prime in javascript?

Infinitbility,Â
JavaScript,Â
June 2nd, 2022 Â· 1 min read

Hi Friends đź‘‹,

Welcome To Infinitbility! âť¤ď¸Ź

To check number is a prime number, looping through 2 to your given number-1 and check is any divisible number or not, if have means itâ€™s not a prime number.

Note: 1 is considered neither prime nor composite.

Today, Iâ€™m going to show How do I check if the number is a prime number or not in Javascript, here I will create a custom function `isPrimeNumber()` where I will check if there have any divisible number based on this it will return a boolean value.

Letâ€™s start the todayâ€™s tutorial How do you check if the number is prime in JavaScript?

In the following example

1. check given value is a number
2. check given value is greater than 1
3. check given value is prime or not
`.css-1chxjt6{position:absolute;right:22px;top:24px;padding:8px 12px 7px;border-radius:5px;color:#6f7177;-webkit-transition:background 0.3s ease;transition:background 0.3s ease;}.css-1chxjt6:hover{background:rgba(255,255,255,0.07);}.css-1chxjt6[data-a11y="true"]:focus::after{content:"";position:absolute;left:-2%;top:-2%;width:104%;height:104%;border:2px solid var(--theme-ui-colors-accent,#6166DC);border-radius:5px;background:rgba(255,255,255,0.01);}@media (max-width:45.9375em){.css-1chxjt6{display:none;}}1// function to check if a number is prime or not2function isPrimeNumber(number) {3  // check if the passed value is a number4  if (typeof number == 'number' && !isNaN(number)) {5    if (number > 1) {6      // looping through 2 to number-17      for (let i = 2; i < number; i++) {8        if (number % i == 0) {9          return false;10          break;11        }12      }13      return true;14    }15  }16  return false;17}1819isPrimeNumber('hello');20isPrimeNumber(44);21isPrimeNumber(3.4);22isPrimeNumber(5);23isPrimeNumber(-3.4);24isPrimeNumber(NaN);`

In the above program, we have taken examples of every possible value like parameter if string, number, positive value, a negative value, or NaN values. letâ€™s check the output.

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

### Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Request New Tutorial or Article on mail [email protected]

### Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

### How to check if a character is a letter or number in JavaScript?

Example of check if a character is a letter or number in javascript with step by step guide.

June 1st, 2022 Â· 1 min read

### How to check if a string is valid url in javascript?

Example of check if a string is valid url in javascript with step by step guide.

June 1st, 2022 Â· 1 min read