Design & Download
Your JS Library
in Seconds!!!

Snippet: isFloat() & isInt()

Written by Christopher West (cwest) on November 22, 2015.
Determine whether or not a value is a number which is also a float or an integer.
function isFloat(x) {
  return typeOf(x, 'Number') && !!(x % 1);
}

function isInt(x) {
  return typeOf(x, 'Number') && x % 1 == 0;
}

In JavaScript there aren't really any differences between floats and integers and for that reason up until ES6 enhancements were added to browsers and other JS environments we didn't have a built-in way to determine if we were dealing with floats or integers. Now, even if you cant be sure that you are working with one of these systems you can still have a way to check if something is a float or an integer thanks to this snippet.

YourJS.isFloat(x) API Documentation

Description

Determines whether or not a number is a float. NOTE: In the true sense of the word, all numbers in JavaScript are floats, but this function determines if a value is a finite number and is not an integer.

Parameters

  1. x {number}:
    The number to be analyzed.

Returns

Returns true if x is a finite non-integer, otherwise false is returned.

YourJS.isInt(x) API Documentation

Description

Determines whether or not a number is an integer. NOTE: In the true sense of the word, all numbers in JavaScript are floats, but this function determines if a value is a finite number which is evenly divisible by 1.

Parameters

  1. x {number}:
    The number to be analyzed.

Returns

Returns true if x is a finite integer, otherwise false is returned.