Number benefits from several changes in ES6, providing several of new methods saving us from writing our own potentially error prone implementation. There are quite a lot of methods so here are some of the ones that are likely to have more use:
Number.isFinite
Determines whether a number is finite (finite means that it could be measured or have a value).
Number.isFinite(Infinity); //falseNumber.isFinite(100); //trueNumber.isInteger
Determines if a number is an integer or not.
Number.isInteger(1); // trueNumber.isInteger(0.1); //falseNumber.isNaN
Before ES6 it was difficult to test if a value was equal to NaN (Not a number). This is because NaN == NaN evaluates to false.
Whilst a global isNaN function has existed in previous versions it has the issue that it converts values which makes it hard to test if something is really NaN:
isNaN("rezha") == true; //trueNumber.isNaN allows you to easily test if a number really is NaN:
Number.isNaN(1); //falseNumber.isNaN(Number.NaN); //trueNumber.EPSILON
Number.EPSILON is the smallest value less than 1 that can be represented as a number and is intended for advanced uses such as testing equality:
Number.EPSILON;//2.220446049250313e-16Number.isSafeInteger
To be considered a safe integer numbers must be able to be represented in a format called IEEE-754 and cannot be the result of rounding any other IEEE-754 number. There are some numbers that fall outside of what can be represented using IEEE-754:
Number.isSafeInteger(3); //truevar unsafe = Math.pow(2, 53);Number.isSafeInteger(unsafe); //falseNumber.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER
IEEE-754 can represent a limited range of numbers. This range can be retrieved using Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER:
Number.MIN_SAFE_INTEGER; //-9007199254740991Number.MAX_SAFE_INTEGER; //9007199254740991