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
1// function to check if a number is prime or not
2function isPrimeNumber(number) {
3 // check if the passed value is a number
4 if (typeof number == 'number' && !isNaN(number)) {
5 if (number > 1) {
6 // looping through 2 to number-1
7 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}
18
19isPrimeNumber('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.

javascript, check if number is prime or not example
javascript, check if number is prime or not example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

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.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

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
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]