Javascript - Number isSafeInteger() Method

The Number.isSafeInteger() method determines whether a value is a safe integer.

Description

The Number.isSafeInteger() method determines whether a value is a safe integer.

A safe integer is an integer that can be represented as an IEEE-754 double precision number (all integers from (2^53 - 1) to -(2^53 - 1))

Syntax

Number.isSafeInteger(value)

Parameter Values

Parameter Require Description
value Required. The value to be tested

Return

A Boolean. Returns true if the value is a safe integer Number, otherwise it returns false

Example

Check whether a value is a safe integer:

Demo

var res = "";
res = res + Number.isSafeInteger(123) + ": 123\n";
res = res + Number.isSafeInteger(-123) + ": -123\n";
res = res + Number.isSafeInteger(5-2) + ": 5-2\n";
res = res + Number.isSafeInteger(0) + ": 0\n";
res = res + Number.isSafeInteger(null) + ": null\n";
res = res + Number.isSafeInteger(0.5) + ": 0.5\n";
res = res + Number.isSafeInteger(Math.pow(2, 53)) + ": 2<sup>53</sup>\n";
res = res + Number.isSafeInteger(Math.pow(2, 53) - 1) + ": 2<sup>53</sup> - 1\n";
res = res + Number.isSafeInteger('123') + ": '123'\n";
res = res + Number.isSafeInteger(false) + ": false\n";
res = res + Number.isSafeInteger(Infinity) + ": Infinity\n";
res = res + Number.isSafeInteger(-Infinity) + ": -Infinity\n";
res = res + Number.isSafeInteger(0 / 0) + ": 0 / 0\n";

console.log(res);// w  w w  .  ja v  a  2 s . co  m

Result