- 3 ways to check if variable is a number in JavaScript
- 1) Using isNan()
- Limitations:
- 2) Using typeof()
- 3) Using Number.isFinite()
- Conclusion:
- Top comments (8)
- Number.isInteger()
- Try it
- Syntax
- Parameters
- Return value
- Description
- Examples
- Using isInteger
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- Check if Number Is Integer JavaScript|Number.isInteger
- Check if Number Is Integer JavaScript|Number.isInteger
- JavaScript Number.isInteger() Method
- Syntax
- Parameters
- Example of the Number.isInterger()
- How to Check if Number Is Integer or Not In JavaScript
- Demo
- Conclusion
- Author Admin
- Javascript is not int
- # Check if a value is a Float or an Integer in JavaScript
- # Checking if a value is an integer
- # A custom function that checks if the value is int or float
- # Checking if the value is a valid integer using RegExp.test()
- # Additional Resources
3 ways to check if variable is a number in JavaScript
I was building a form the other day in Vue and I had to write number validation for the field so had to write the logic to check whether the input value is number or not. I wanted to list out the few ways I learnt which might be helpful to others.
NOTE: In JavaScript, special values like NaN , Infinity and -Infinity are numbers too — though, we’ll be ignoring those values.
1) Using isNan()
The isNaN() determines whether a value is NaN or not. We can take advantage of this to determine whether a variable is number type or not.
var numberOfpushUpsToday = 34; if(!isNaN(numberOfpushUpsToday)) console.log('It is a number') > else console.log('It is not a number') >
!isNaN(34) // returns true !isNaN('34') // returns true !isNaN('Hello') // returns false !isNaN(true) // returns true !isNaN(false) // returns true !isNaN('undefined') // returns false
Limitations:
1) It returns true for the Boolean values because the Boolean values are converted to numbers 0 and 1 accordingly so it would be misleading sometimes.
2) We have same issue for the null value as well. It returns true and hence one has to be careful while writing the code.
2) Using typeof()
num = 45 strng = '34' typeof num // returns 'number' typeof strng // returns "string" typeof undefined // returns "undefined" typeof null // returns "object"
If the variable is type number, it would return the string number . We can use this to determine if the variable is number type.
var numberOfpushUpsToday = 34; if(typeof numberOfpushUpsToday === 'number' ) console.log('It is a number') > else console.log('It is not a number') >
The typeof() performs much better than isNaN() . It correctly determines that a string variable, null and Boolean values are not numbers.
3) Using Number.isFinite()
The function isFinite() determines if the passed value is finite. The arguments are first converted to numbers and then checks if the value is finite and hence this is the most best way among all the methods mentioned above.
Number.isFinite(34) // returns true Number.isFinite('Hello') // returns false Number.isFinite(undefined) // returns false Number.isFinite(true) // returns false Number.isFinite(null) // returns false
var numberOfpushUpsToday = 34; if(Number.isFinite(numberOfpushUpsToday) ) console.log('It is a number') > else console.log('It is not a number') >
Conclusion:
Although these methods can get tricky with Boolean values, undefined and null , they are helpful in solving some problems in day to day life. One just have to be careful while writing the code according to their needs. And that sums it up.
Thank you..
Comment below if you have any feedback or thoughts.
Top comments (8)
I’m a self-taught dev focused on websites and Python development. My friends call me the «Data Genie». When I get bored, I find tech to read about, write about and build things with.
Number.isInteger()
The Number.isInteger() static method determines whether the passed value is an integer.
Try it
Syntax
Parameters
The value to be tested for being an integer.
Return value
The boolean value true if the given value is an integer. Otherwise false .
Description
If the target value is an integer, return true , otherwise return false . If the value is NaN or Infinity , return false . The method will also return true for floating point numbers that can be represented as integer. It will always return false if the value is not a number.
Note that some number literals, while looking like non-integers, actually represent integers — due to the precision limit of ECMAScript floating-point number encoding (IEEE-754). For example, 5.0000000000000001 only differs from 5 by 1e-16 , which is too small to be represented. (For reference, Number.EPSILON stores the distance between 1 and the next representable floating-point number greater than 1, and that is about 2.22e-16 .) Therefore, 5.0000000000000001 will be represented with the same encoding as 5 , thus making Number.isInteger(5.0000000000000001) return true .
In a similar sense, numbers around the magnitude of Number.MAX_SAFE_INTEGER will suffer from loss of precision and make Number.isInteger return true even when it’s not an integer. (The actual threshold varies based on how many bits are needed to represent the decimal — for example, Number.isInteger(4500000000000000.1) is true , but Number.isInteger(4500000000000000.5) is false .)
Examples
Using isInteger
.isInteger(0); // true Number.isInteger(1); // true Number.isInteger(-100000); // true Number.isInteger(99999999999999999999999); // true Number.isInteger(0.1); // false Number.isInteger(Math.PI); // false Number.isInteger(NaN); // false Number.isInteger(Infinity); // false Number.isInteger(-Infinity); // false Number.isInteger("10"); // false Number.isInteger(true); // false Number.isInteger(false); // false Number.isInteger([1]); // false Number.isInteger(5.0); // true Number.isInteger(5.000000000000001); // false Number.isInteger(5.0000000000000001); // true, because of loss of precision Number.isInteger(4500000000000000.1); // true, because of loss of precision
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Feb 21, 2023 by MDN contributors.
Your blueprint for a better internet.
Check if Number Is Integer JavaScript|Number.isInteger
JavaScript check if number is integer; In this tutorial, you will learn how to check if number is integer in javaScript.
When you work with javascript. Most times you check that value is string, number, integer number, decimal number, etc. So In this tutorial, you will learn how you can check value is a number or not in javascript using the number.isInteger() method.
Check if Number Is Integer JavaScript|Number.isInteger
You can easily check if a number is an integer or not in javascript using the number.isInteger() function.
JavaScript Number.isInteger() Method
In JavaScript Number.isInteger() method works with numbers, it will check whether a value an integer. This method returns true if the value is of the type Number and an integer. Otherwise, it will return false.
If the target value is an integer, return true, otherwise, return false. If the value is NaN or Infinity, return false. The method will also return true for floating-point numbers that can be represented as an integer.
Syntax
The syntax of number.isInterger() method is:
Parameters
This method accept only one parameter.
Example of the Number.isInterger()
You can use this below source code to check a given number is an integer or not in javascript.
We will use javascript number method, The Number() the method converts a string to a number.
How to Check if Number Is Integer or Not In JavaScript
Demo
How to Check if Number Is Integer or Not In JavaScript
Conclusion
In this javascript tutorial of the Number.isInteger() method, you have learned how you can check a number is an integer or not in javascript using the is Number.isInteger() method. In javaScript Number.isInteger() function to check whether the value passed to it is an integer or not. It returns true if the passed value is an integer, otherwise, it will return false.
If you have any questions or thoughts to share, use the comment form below to reach us.
Author Admin
My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.
Javascript is not int
Last updated: Dec 27, 2022
Reading time · 4 min
# Check if a value is a Float or an Integer in JavaScript
To check if a value is a float or an integer:
- Use the Number.isInteger() method to check if a value is an integer.
- Check if the value has a type of number and is not an integer or NaN to check if the value is a float.
Copied!// ✅ check if a value is a float function isFloat(value) if ( typeof value === 'number' && !Number.isNaN(value) && !Number.isInteger(value) ) return true; > return false; > console.log(isFloat(1)); // 👉️ false console.log(isFloat(1.5)); // 👉️ true console.log(isFloat(-1.5)); // 👉️ true console.log(isFloat('1.5')); // 👉️ false
To check if a number is a float, check if the value has a type of number and is not an integer or NaN .
We first check if the provided value has a type of number. If it doesn’t we return false straight away.
Copied!function isFloat(value) if ( typeof value === 'number' && !Number.isNaN(value) && !Number.isInteger(value) ) return true; > return false; >
We used the logical AND (&&) operator to check for multiple conditions. For our if block to run, all conditions have to be met.
The second condition checks if a value isn’t NaN (not a number). Unfortunately, NaN has a type of number in JavaScript.
Copied!console.log(typeof Number.NaN); // 👉️ number
If the provided value is of type number , is not NaN and is not an integer, then the value is a floating-point number.
# Checking if a value is an integer
We used the Number.isInteger() method to check if a value is an integer.
Copied!console.log(Number.isInteger(1)); // 👉️ true console.log(Number.isInteger('1')); // 👉️ false console.log(Number.isInteger(1.5)); // 👉️ false
There is a catch when using the Number.isInteger() method.
It returns true if the passed-in value:
Here’s an example of a float that can be represented as an integer.
Copied!console.log(Number.isInteger(10.0)); // 👉️ true
It depends on your use case whether you consider 10.0 to be an integer or float.
Our implementation of the function considers numbers like 1.0 and 5.0 to be integers.
# A custom function that checks if the value is int or float
You can combine the conditions into a single function to check if a value is a float or an integer.
Copied!function isIntOrFloat(value) if (typeof value === 'number' && !Number.isNaN(value)) if (Number.isInteger(value)) console.log('The value is an integer', value); > else console.log('The value is a floating-point number', value); > > else console.log('The value is not a number', value); > > isIntOrFloat(1); // 👉️ The value is an integer 1 isIntOrFloat(1.5); // 👉️ The value is a floating-point number 1.5 isIntOrFloat('1'); // 👉️ The value is not a number 1 isIntOrFloat(5.0); // 👉️ The value is an integer 5 isIntOrFloat('test'); // 👉️ The value is not a number test
The function first checks if the value is a number and is not NaN because NaN has a type of number in JavaScript.
If we enter the if block, we know that the value is a number.
The next step is to check if the value is an integer using the Number.isInteger() function.
If the value is an integer , the if block runs.
If the value is a number and is not an integer, then the value is a floating-point number and the else block runs.
If the initial conditional check fails and the value does not have a type of number or is NaN , then the value is not a number.
# Checking if the value is a valid integer using RegExp.test()
You can also use the RegExp.test() method to check if a value is a valid integer.
Copied!function isInteger(value) return /^-?9+$/.test(value); > console.log(isInteger(12)); // 👉️ true console.log(isInteger(-12)); // 👉️ true console.log(isInteger(1.0)); // 👉️ true console.log(isInteger('1')); // 👉️ true console.log(isInteger(1.5)); // 👉️ false
The RegExp.test method matches the specified regular expression against the supplied value and returns true if the regular expression is matched in the string and false otherwise.
The forward slashes / / mark the beginning and end of the regular expression.
The caret ^ matches the beginning of the input and the dollar sign $ matches the end of the input.
We used a hyphen to also allow for negative integers.
The question mark ? matches the preceding item (the minus) 0 or 1 times. In other words, the minus — might be there, or it might not be there.
The character class 4 matches the digits in the range.
The plus + matches the preceding item (the range of digits) one or more times.
If you ever need help reading a regular expression, check out this regular expression cheatsheet by MDN.
It contains a table with the name and the meaning of each special character with examples.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.