Navigate back to the homepage

How to check if number is integer or float in javascript?

Infinitbility
JavaScript
June 1st, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To check number is an integer or float, JavaScript provides isInteger() method just pass your number as a parameter and it will return true if the number is an integer or else false.

Today, I’m going to show How do I check if the number is an integer or float in Javascript, here I will use the javascript string isInteger() method, to check if a number is an integer or float.

What is a isInteger() method?

The Number.isInteger() method determines whether the passed value is an integer.

Let’s start the today’s tutorial How do you check if the number is integer or float in JavaScript?

In the following example, we will create our custom function to check number is int or float, where we will also add some more conditions like the given parameter should be a number and after that, we will check number is integer or float.

1// function to check if a number is a float or integer value
2function validateNumber(x) {
3 // check if the passed value is a number
4 if(typeof x == 'number' && !isNaN(x)){
5 // check if it is integer
6 if (Number.isInteger(x)) {
7 console.log(`${x} is integer.`);
8 }
9 else {
10 console.log(`${x} is a float value.`);
11 }
12 } else {
13 console.log(`${x} is not a number`);
14 }
15}
16
17validateNumber('hello');
18validateNumber(44);
19validateNumber(3.4);
20validateNumber(-3.4);
21validateNumber(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 integer or float example
javascript, check if number is integer or float 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 the first letter of a string is uppercase in javascript?

Example of check if the first letter of a string is uppercase in javascript with step by step guide.

May 31st, 2022 · 1 min read

how to check if the first character of a string is a letter in javascript?

Example of check if the first character of a string is a letter in javascript with step by step guide.

May 31st, 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]