JavaScript(JS) number method - issafeinteger
The Number.isSafeInteger
method in JavaScript is a static method that checks whether a given value is a safe integer or not. A safe integer is an integer that can be exactly represented as a double-precision floating-point number. It returns true
if the value is a safe integer, and false
otherwise.
Here's an example usage:
console.log(Number.isSafeInteger(5)); // true console.log(Number.isSafeInteger(5.0)); // true console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER)); // true console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1)); // false console.log(Number.isSafeInteger("5")); // falseSource:wwi.wgiftidea.com
In the above example, the Number.isSafeInteger
method is used to check whether the given values are safe integers or not. The first two calls return true
because 5
and 5.0
are both safe integers. The third call returns true
because Number.MAX_SAFE_INTEGER
is a safe integer. The fourth call returns false
because Number.MAX_SAFE_INTEGER + 1
is not a safe integer. The fifth call returns false
because "5"
is a string and not a number.