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:
Determines whether a number is finite (finite means that it could be measured or have a value).
Number.isFinite(Infinity); //false Number.isFinite(100); //true
Determines if a number is an integer or not.
Number.isInteger(1); // true Number.isInteger(0.1); //false
Before ES6 it was difficult to test if a value was equal to
NaN (Not a number). This is because
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
isNaN("rezha") == true; //true
Number.isNaN allows you to easily test if a number really is
Number.isNaN(1); //false Number.isNaN(Number.NaN); //true
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:
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); //true var unsafe = Math.pow(2, 53); Number.isSafeInteger(unsafe); //false
Number.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; //-9007199254740991 Number.MAX_SAFE_INTEGER; //9007199254740991