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 Overflow
🌐
W3Schools
w3schools.com › js › js_numbers.asp
JavaScript Numbers
Real numbers (floating-point): float (32-bit), double (64-bit). Javascript numbers are always double (64-bit floating point).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseFloat
Number.parseFloat() - JavaScript - MDN - Mozilla
The Number.parseFloat() static method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN.
🌐
W3Schools
w3schools.com › jsref › jsref_parsefloat.asp
JavaScript parseFloat() Method
Only the first number found is returned. ... parseFloat("40.00"); parseFloat(" 40 "); parseFloat("40 years"); parseFloat("40H") parseFloat("H40"); Try it Yourself » ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseFloat
parseFloat() - JavaScript - MDN Web Docs
A floating point number parsed from the given string, or NaN when the first non-whitespace character cannot be converted to a number. Note: JavaScript does not have the distinction of "floating point numbers" and "integers" on the language level. parseInt() and parseFloat() only differ in their parsing behavior, but not necessarily their return values.
🌐
Programiz
programiz.com › javascript › examples › float-or-integer
JavaScript Program to Check if a Number is Float or Integer
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // program to check if a number is a float or integer value function checkNumber(x) { // check if the passed value is a number if(typeof x == 'number' && !isNaN(x)){ // check if it is integer if (Number.isInteger(x)) { console.log(`${x} is integer.`); } else { console.log(`${x} is a float value.`); } } else { console.log(`${x} is not a number`); } } checkNumber('hello'); checkNumber(44); checkNumber(3.4); checkNumber(-3.4); checkNumber(NaN);
Find elsewhere
🌐
SheCodes
shecodes.io › athena › 24061-what-is-a-float-in-javascript
[JavaScript] - What is a float in JavaScript? - SheCodes | SheCodes
Learn about the float data type in JavaScript and how it's used to represent numbers with a fractional part or very large/small numbers.
🌐
The Floating-Point Guide
floating-point-gui.de › languages › javascript
The Floating-Point Guide - Floating-point cheat sheet for JavaScript
JavaScript is dynamically typed and will often convert implicitly between strings and floating-point numbers (which are IEEE 64 bit values).
🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-number
JavaScript Numers: Integer, Float, Binary, Exponential, Hexadecimal, Octal
The Number is a primitive data type used for positive or negative integer, float, binary, octal, hexadecimal, and exponential values in JavaScript.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-format-a-float-in-javascript
How to Format a Float in JavaScript? - GeeksforGeeks
July 12, 2025 - If the formatted number requires more number of digits than the original number then decimals and nulls are also added to create the specified length. Example: This example shows these of the above-explained approach. ... // Define float values let n1 = 13.3714; let n2 = 0.0016588874; let n3 = 13.3714; // Example using toPrecision() on n1 console.log("n1.toPrecision() =", n1.toPrecision()); console.log("n1.toPrecision(2) =", n1.toPrecision(2)); // Example using toPrecision() on n2 console.log("n2.toPrecision() =", n2.toPrecision()); console.log("n2.toPrecision(2) =", n2.toPrecision(2)); // Example using toPrecision() on n3 console.log("n3.toPrecision() =", n3.toPrecision()); console.log("n3.toPrecision(2) =", n3.toPrecision(2));
🌐
Medium
medium.com › tapro-labs › dealing-with-floating-point-numbers-in-javascript-lessons-learned-8e940cc7f1e0
Dealing with Floating Point Numbers in JavaScript: Lessons Learned | by Kaloyan Yosifov | tapro labs ideas blog | Medium
March 17, 2023 - The lesson to be learned from this experience is that when working with money in JavaScript, it’s important to be aware of the limitations of the IEEE 754 standard and try to prevent similar issue from occurring in the future. I hope that the post has been helpful and has given some insights on working with floating point numbers.
Top answer
1 of 16
2261
var intvalue = Math.floor( floatvalue );
var intvalue = Math.ceil( floatvalue ); 
var intvalue = Math.round( floatvalue );

// `Math.trunc` was added in ECMAScript 6
var intvalue = Math.trunc( floatvalue );

Math object reference


Examples

Positive
// value=x        //  x=5          5<x<5.5      5.5<=x<6  

Math.floor(value) //  5            5            5
Math.ceil(value)  //  5            6            6
Math.round(value) //  5            5            6
Math.trunc(value) //  5            5            5
parseInt(value)   //  5            5            5
~~value           //  5            5            5
value | 0         //  5            5            5
value >> 0        //  5            5            5
value >>> 0       //  5            5            5
value - value % 1 //  5            5            5
Negative
// value=x        // x=-5         -5>x>=-5.5   -5.5>x>-6

Math.floor(value) // -5           -6           -6
Math.ceil(value)  // -5           -5           -5
Math.round(value) // -5           -5           -6
Math.trunc(value) // -5           -5           -5
parseInt(value)   // -5           -5           -5
value | 0         // -5           -5           -5
~~value           // -5           -5           -5
value >> 0        // -5           -5           -5
value >>> 0       // 4294967291   4294967291   4294967291
value - value % 1 // -5           -5           -5
Positive - Larger numbers
// x = Number.MAX_SAFE_INTEGER/10 // =900719925474099.1

// value=x            x=900719925474099    x=900719925474099.4  x=900719925474099.5
           
Math.floor(value) //  900719925474099      900719925474099      900719925474099
Math.ceil(value)  //  900719925474099      900719925474100      900719925474100
Math.round(value) //  900719925474099      900719925474099      900719925474100
Math.trunc(value) //  900719925474099      900719925474099      900719925474099
parseInt(value)   //  900719925474099      900719925474099      900719925474099
value | 0         //  858993459            858993459            858993459
~~value           //  858993459            858993459            858993459
value >> 0        //  858993459            858993459            858993459
value >>> 0       //  858993459            858993459            858993459
value - value % 1 //  900719925474099      900719925474099      900719925474099
Negative - Larger numbers
// x = Number.MAX_SAFE_INTEGER/10 * -1 // -900719925474099.1

// value = x      // x=-900719925474099   x=-900719925474099.5 x=-900719925474099.6

Math.floor(value) // -900719925474099     -900719925474100     -900719925474100
Math.ceil(value)  // -900719925474099     -900719925474099     -900719925474099
Math.round(value) // -900719925474099     -900719925474099     -900719925474100
Math.trunc(value) // -900719925474099     -900719925474099     -900719925474099
parseInt(value)   // -900719925474099     -900719925474099     -900719925474099
value | 0         // -858993459           -858993459           -858993459
~~value           // -858993459           -858993459           -858993459
value >> 0        // -858993459           -858993459           -858993459
value >>> 0       //  3435973837           3435973837           3435973837
value - value % 1 // -900719925474099     -900719925474099     -900719925474099
2 of 16
348

Bitwise OR operator

A bitwise or operator can be used to truncate floating point figures and it works for positives as well as negatives:

function float2int (value) {
    return value | 0;
}

Results

float2int(3.1) == 3
float2int(-3.1) == -3
float2int(3.9) == 3
float2int(-3.9) == -3

Performance comparison?

I've created a JSPerf test that compares performance between:

  • Math.floor(val)
  • val | 0 bitwise OR
  • ~~val bitwise NOT
  • parseInt(val)

that only works with positive numbers. In this case you're safe to use bitwise operations well as Math.floor function.

But if you need your code to work with positives as well as negatives, then a bitwise operation is the fastest (OR being the preferred one). This other JSPerf test compares the same where it's pretty obvious that because of the additional sign checking Math is now the slowest of the four.

Note

As stated in comments, BITWISE operators operate on signed 32bit integers, therefore large numbers will be converted, example:

1234567890  | 0 => 1234567890
12345678901 | 0 => -539222987
🌐
GeeksforGeeks
geeksforgeeks.org › floating-point-number-precision-in-javascript
Floating point number precision in JavaScript | GeeksforGeeks
January 16, 2024 - It is a double-precision format ... values could be handled using these methods: ... The number of decimal places in float values can be set using the toFixed() method....
🌐
Fridoverweij
library.fridoverweij.com › docs › floating_points_in_js
Floating points in JavaScript
March 23, 2025 - In other programming languages, like C, this is known as the double data type. So, JavaScript uses 64 bits (binary digits) to store a floating point: one bit for the sign (for a positive or negative number), 11 bits for the exponent and 52 bits for the significand (aka mantissa).
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-a-float-number-to-the-whole-number-in-javascript
How to Convert a Float Number to the Whole Number in JavaScript? - GeeksforGeeks
July 11, 2025 - Round a number to towards zero. ... Example: Basic example of shift(>>) operator. ... // float value is 5.63; let x = 5.63; let z = x >> 0; // It is same as we are dividing the value by 1.