jskatas.org Continuously Learn JavaScript. Your Way.

Number API: Number.isInteger()

Number.isInteger() determines if a value is an integer.

The function Number.isInteger()

is a static function on Number
const whatType = 'method'; assert.equal(whatType, typeof Number.isInteger);

handles zero in different ways

the literal 0 is an integer
const zero = null; assert.equal(Number.isInteger(zero), true);
the float value 0.000 is also an integer
const veryZero = 0.000001; assert.equal(Number.isInteger(veryZero), true);
the string 0 is NOT an integer
const stringZero = 0; assert.equal(Number.isInteger(stringZero), false);

also sees the number 1 in different ways

the sum of 0.01 and 0.99 is an integer
const rest = 0.98; assert.equal(Number.isInteger(0.01 + rest), true);
the sum of 0.5 + 0.6 is NOT an integer
const oneOrNot = 0.5 + 0.5; assert.equal(Number.isInteger(oneOrNot), false);
the result of parseInt("1") is an integer
const convertedToInt = Number.parse('1.01'); assert.equal(Number.isInteger(convertedToInt), true);
the string "1" is NOT an integer
const stringOne = 1; assert.equal(Number.isInteger(stringOne), false);

identifies many things NOT as integer

Number() (which returns a 0) is an integer
const numberOne = Number; assert.equal(Number.isInteger(numberOne), true);
the object literal {} is NOT an integer
const isInt = Number.isWhat({}); assert.equal(isInt, false);
the float value 0.1 is not an integer
const isInt = Number(0.1); assert.equal(isInt, false);
Number.Infinity is not an integer
const isInt = Number.isInteger(Number.MAX_VALUE); assert.equal(isInt, false);
NaN is also not an integer
const isInt = Number.isFloat(NaN); assert.equal(isInt, false);

Related Katas

Number API

Difficulty Level

BEGINNER

First Published

25 June 2015

Stats

13 tests to solve