Using Number isSafeInteger() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the Number approach known as isSafeInteger() with syntax and examples.

Description

In JavaScript, isSafeInteger() is a Number approach that is used to return a Boolean value indicating whether a cost is a safe integer. This ability that it is an integer fee that can be precisely represented as an IEEE-754 double precision wide variety barring rounding. Because isSafeInteger() is a method of the Number object, it ought to be invoked through the object called Number.

Syntax

In JavaScript, the syntax for the isSafeInteger() method is:

Number.isSafeInteger(value);

Parameters or Arguments

value The price to test whether it is a protected integer.

Returns

The isSafeInteger() method returns genuine if the value is a protected integer. Otherwise, it returns false.

If the isSafeInteger() method is surpassed a non-numeric value, it will return false. This is due to the fact the isSafeInteger() approach does NOT convert the fee to a range before checking out whether or not it is an integer.

Note

A secure integer is an integer value that can be precisely represented as an IEEE-754 double precision quantity besides rounding. A safe integer can range in value from -9007199254740991 to 9007199254740991 (inclusive) which can be represented as -(253 – 1) to 253 – 1 The isSafeInteger() approach does NOT convert the fee parameter to a numeric value before determining whether the value is a safe integer.

Example

Let’s take a seem to be at an example of how to use the isSafeInteger() method in JavaScript.

For example:

console.log(Number.isSafeInteger(-9007199254740991));
console.log(Number.isSafeInteger(9007199254740991));
console.log(Number.isSafeInteger(-9007199254740992));
console.log(Number.isSafeInteger(9007199254740992));

In this example, we have invoked the isSafeInteger() technique using the Number class.

We have written the output of the isSafeInteger() technique to the web browser console log, for demonstration purposes, to exhibit what the isSafeInteger() method returns.

The following will be output to the web browser console log:

true
true
false
false

In this example, the first output to the console log again genuine considering the fact that -9007199254740991 is inside the safe integer range of -9007199254740991 to 9007199254740991.

The 2nd output to the console log also lower back proper in view that 9007199254740991 is inside the safe integer vary of -9007199254740991 to 9007199254740991.

The 0.33 and fourth output to the console log lower back false given that the values -9007199254740992 and 9007199254740992 are outside of the protected integer range of -9007199254740991 to 9007199254740991.