What is the difference between Number.isInteger() and Number.isSafeInteger() in JavaScript ?

MoreOnFew
Posted underJavascript

As a part of ES6 (also known as ECMAScript 2015 or ECMAScript 6), there are a couple of new JavaScript features and methods introduced. Today we would be concentrating on just two of them i.e Number.isInteger() and Number.isSafeInteger().

What is Number.isInteger() ?

The Number.isInteger() is a new method of the Number object introduced as a part of ES6. This is an interesting and useful feature that can help you save some time with mathematical operations. The Number.isInteger() method basically tells you if the number passed on to the method as a parameter or argument is an Integer or not. So say if I pass a number that is Integer to the method it would return a boolean true. However, if I pass anything else to it, it would return false. Let’s take a look at an example:

Number.isInteger(10);
//returns true

Number.isInteger(13.5);
//returns false

Please note that the Number.isInteger() method returns a Boolean value always.

Number.isInteger() should not be confused with the parseInt() method. The parseInt() method parses the parameter string to a number (assuming the data type of the parameter ‘number’ was string and you want JavaScript to understand it as an Integer), and return the value as an integer. If it cannot parse it as a number, it returns NaN (Not a Number). Number.isInteger() on the other hand checks if the parameter number passed to it is an integer already and returns true or false accordingly.

What is Number.isSafeInteger() ?

The Number.isSafeInteger() on the other hand, is a method that is very similar to the Number.isInteger(), however, it checks if the value is a “Safe Integer”, Safe integers are all integers from -(253 – 1) to +(253 – 1) i.e -9007199254740991 to 9007199254740991. Let’s take a look at an example:

Number.isSafeInteger(10);    // returns true

Number.isSafeInteger(12345678901234567890);  // returns false

Number.isSafeInteger(9007199254740991);  // returns true

Number.isSafeInteger(9007199254740992);  // returns false

Please note that Number.isSafeInteger() too returns a Boolean value (true or false) always.

You can find out the range of Safe Integer using two new properties of the number object which are Number.MIN_SAFE_INTEGER (returns -9007199254740991) and Number.MAX_SAFE_INTEGER (returns 9007199254740991) .

But, What is a Safe Integer anyway?

“Safe Integer” is not a mathematical term and hence if you ask your Mathematics Genius friend about it he/she might not be aware of it at all. The “Safe Integer” here talks about Integers that are safe and stable to be used in JavaScript. JavaScript numbers have only enough storage space to represent integers in the range of -(253 – 1) to +(253 – 1), beyond this point the JavaScript integers are unsafe or unstable. Beyond 253 , JavaScript can represent only every second mathematical integer. For example :

console.log(Math.pow(2, 53));
//9007199254740992

console.log(9007199254740992);
//9007199254740992

console.log(9007199254740993);
// 9007199254740992 Surprising!!

console.log(9007199254740994);
//9007199254740994 fine !

console.log(9007199254740995);
// 9007199254740996

Hence you require Number.isSafeInteger() to find out if the integer is safe or not in JavaScript’s context.

There are other new Math and Number related properties and Methods that ES6 has introduced and I would recommend that you try those out too along with the Number.isInteger(); and the Number.isSafeInteger() methods. Do let us know if you learned anything new today.

TaggedECMAScriptES6JavascriptNumber


MoreOnFew.com – Read tutorials related to JavaScript, jQuery, HTML5, CSS3 and more !

Cover Image for How to check if checkbox is checked in jQuery?

How to check if checkbox is checked in jQuery?

It is very easy to check if a checkbox is checked in jQuery using the .is() or .prop() method of jQuery combined with the ‘:checked’ selector. There are other ways too like checking for the attribute checked value, however, they aren’t very reliable. The easiest and most stable way to check if checkbox is checked … Read more

MoreOnFew
Cover Image for [Solved] Attempted import error: ‘Switch’ is not exported from ‘react-router-dom’

[Solved] Attempted import error: ‘Switch’ is not exported from ‘react-router-dom’

You might be facing an “Attempted import error: ‘Switch’ is not exported from ‘react-router-dom’ ” error if you are using the react-router-dom package version 6. This error is caused due to using the older switch syntax of the react-router-dom. From version 6 onwards, the react-router-dom has replaced “Switch” with “Routes”. Let us take a look … Read more

MoreOnFew