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 which 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:

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 had 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 which 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:

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 about it at all. The “Safe Integer” here talks about Integers which 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 :

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 learnt anything new today.

Related Posts

  • How to replace all occurrences of a string in JavaScript? Out of the different "Data Types" in JavaScript, String has been one of my favorites. It is also one of the most widely used Data Type. It would not be an […]
  • How to remove multiple attributes in jquery? Many a time you might have come across a scenario where you would want to remove multiple attributes from an element. For example, consider the following DIV, <div […]
  • How to set a timer in JavaScript? Quite often you might come across scenarios wherein you would need to have a timer set in JavaScript. For instance, you might want to call a function after a delay of […]
  • How to search for a string in JavaScript? The String data type must be the most common data type that we use in JavaScript. One of the most common functionality around string that we would have come across is […]
  • What are Polyfills in Javascript ? With HTML5 getting popular, a large number of developers have moved on to HTML5 based development. It has indeed helped developers to implement extra features as well […]
  • How to make placeholder work in IE ? One of the interesting features of HTML5 is the "placeholder" attribute. This new attribute has helped web developers save a lot of time as without it we would have had […]