check for a remainder when dividing by 1:
function isInt(n) {
return n % 1 === 0;
}
If you don't know that the argument is a number you need two tests:
function isInt(n){
return Number(n) === n && n % 1 === 0;
}
function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}
Update 2019 5 years after this answer was written, a solution was standardized in ECMA Script 2015. That solution is covered in this answer.
Answer from kennebec on Stack Overflowcheck for a remainder when dividing by 1:
function isInt(n) {
return n % 1 === 0;
}
If you don't know that the argument is a number you need two tests:
function isInt(n){
return Number(n) === n && n % 1 === 0;
}
function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}
Update 2019 5 years after this answer was written, a solution was standardized in ECMA Script 2015. That solution is covered in this answer.
There is a method called Number.isInteger() which is currently implemented in everything but IE. MDN also provides a polyfill for other browsers:
Number.isInteger = Number.isInteger || function(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value;
};
However, for most uses cases, you are better off using Number.isSafeInteger which also checks if the value is so high/low that any decimal places would have been lost anyway. MDN has a polyfil for this as well. (You also need the isInteger pollyfill above.)
if (!Number.MAX_SAFE_INTEGER) {
Number.MAX_SAFE_INTEGER = 9007199254740991; // Math.pow(2, 53) - 1;
}
Number.isSafeInteger = Number.isSafeInteger || function (value) {
return Number.isInteger(value) && Math.abs(value) <= Number.MAX_SAFE_INTEGER;
};
To convert a string to a Number you can use the Number constructor:
const n = Number('my string') // NaN ("not a number")
To check to see if the conversion was a valid number:
Number.isNaN(n)
Note: Internet Explorer does not support Number.isNaN and for that you can use the older isNaN. More here and here.
To check if a Number is an integer, there are two built-in methods:
Number.isInteger(), andNumber.isSafeInteger()
Further discussion can be found here.
let a = [4, 2.3, "SO2", 4, "O2", 3.4, 4.5, "CO2", 5.6, 3.4, 2];
a.forEach((e)=>{
const n = Number(e)
if (Number.isNaN(n)) {
console.log(e + " :letter")
return
}
if (Number.isInteger(n)) {
console.log(e + " :integer")
return
}
console.log(e + " :non-integer number")
})
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Note
“Floating point” is the name of the technology used to represent the numbers in binary. This is why I changed the terminology from "float" to "non-integer number" in the code above.
Some numeric values in JavaScript contain non-numeric characters. eg.
Infinity
0x3d // hexadecimal
3n // BigInt
0b101011 // Binary
1e2 // times ten to the power of...
There are now two kinds of number in JavaScript: Number and BigInt.
Number is an implementation of the IEEE 754 32-bit floating point number specification.
This should Work
function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}
After all the improvements/features and fixes which were made to javascript (or precisely ECMAScript) since ES6 (ES2015) till now (ES2022), isn't it about time to fix the ultimate pending weakness i.e. having an Integer and a Float type as well (just like "var" keyword was/is there but they added "let" and "const" as a better semantics and good practice) and not just mashup everything in Number.
Heck, we even got a big int to represent big integers (no floats allowed) but we still don't have Integer's and Floats which are SUPER useful in almost every scenario.
So, why is it still not added and not planned as well? Those are VERY important data types and MOST languages HAVE those data types as they are NEEDED, why is it not planned for ECMAScript? Is it planned? Do you want to see this added?