Number isSafeInteger() Method - Javascript Number

Javascript examples for Number:isSafeInteger

Description

The Number.isSafeInteger() method returns true if the value is of the type Number, and a safe integer. Otherwise it returns false.

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

Parameter Values

Parameter Description
value Required. The value to be tested

Return Value:

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

The following code shows how to Check whether a value is a safe integer:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*  ww  w.j a  v  a  2s .c o m*/
    var res = "";
    res = res + Number.isSafeInteger(123) + ": 123<br>";
    res = res + Number.isSafeInteger(-123) + ": -123<br>";
    res = res + Number.isSafeInteger(5-2) + ": 5-2<br>";
    res = res + Number.isSafeInteger(0) + ": 0<br>";
    res = res + Number.isSafeInteger(0.5) + ": 0.5<br>";
    res = res + Number.isSafeInteger(Math.pow(2, 53)) + ": 2<sup>53</sup><br>";
    res = res + Number.isSafeInteger(Math.pow(2, 53) - 1) + ": 2<sup>53</sup> - 1<br>";
    res = res + Number.isSafeInteger('123') + ": '123'<br>";
    res = res + Number.isSafeInteger(false) + ": false<br>";
    res = res + Number.isSafeInteger(Infinity) + ": Infinity<br>";
    res = res + Number.isSafeInteger(-Infinity) + ": -Infinity<br>";
    res = res + Number.isSafeInteger(0 / 0) + ": 0 / 0<br>";

    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

Related Tutorials