ES6 Number has several methods and properties to perform numeric functions that has the date, floating points, integers, etc. Using Numbers in ES6, we can easily work with the Number objects. It is because the browser automatically converts number literals to instances of the number class.
The Number object is created by using the Number() constructor. Some of the primary uses of the Number object include NaN, which will return when the argument cannot be converted into a number.
var num = new Number(value);
Value: It is the numeric value of the object being created. If we give any non-number argument in place of it, then it returns NaN because the corresponding argument cannot be converted into a number.
Let us see some of the properties of Number object which are introduced in ES6 are tabulated as follows:
S.no | Properties | Description |
---|---|---|
1. | Number.EPSILON | It defines the smallest intervals between two representable numbers. |
2. | Number.MAX_SAFE_INTEGER | It defines maximum safe integer in JavaScript (253-1) |
3. | Number.MAX_VALUE | It defines the largest possible representable number. |
4. | Number.MIN_SAFE_INTEGER | It defines the minimum safe integer in JavaScript (-(253-1)). |
5. | Number.MIN_VALUE | It defines the smallest positive number, which is closest to zero. |
6. | Number.Nan | It defines 'not a number' value. |
7. | Number.NEGATIVE_INFINITY | It defines a value, which is less than the defined number. |
8. | Number.POSITIVE_INFINITY | It defines a value, which is greater than the defined number. |
9. | Number.prototype | It defines a special value that represents infinity. |
Let us try to elaborate on the Number properties introduced in ES6.
This property represents the difference between 1 and the smallest floating-point number, which is greater than 1. We do not have to create a Number object for accessing the static property because we can simply use Number.EPSILON property.
var value = Number.EPSILON; console.log(value);
This property represents the maximum safe integer in JavaScript (253-1).
var value = Number.MAX_SAFE_INTEGER; console.log(value);
This property belongs to the static Number object and represents constants for the largest possible positive numbers.
var val = Number.MAX_VALUE; console.log("Number.MAX_VALUE equals to: " + val );
It represents the minimum safe integer in JavaScript (-(253-1)).
var val = Number.MIN_SAFE_INTEGER; console.log("Number. MIN_SAFE_INTEGER equals to: " + val );
It represents the smallest positive numeric value.
var val = Number.MIN_VALUE; console.log("Number.MIN_VALUE equals to : " + val );
The Number object contains only default methods that are part of every object's definition. The methods of the number object are tabulated as follows:
S.no. | Methods | Description |
---|---|---|
1. | Number.isNan() | It returns whether the passed value is NaN or not. |
2. | Number.isFinite() | It returns whether the passed value is a finite number. |
3. | Number.isInteger() | It returns whether the passed value is an integer. |
4. | Number.isSafeInteger() | It determines whether the passed value is a safe integer (range between -(253 - 1) and (253-1)). |
5. | Number.parseFloat() | It is equivalent to the parseFloat() of the global object. |
6. | Numbr.pareInt() | It is equivalent to the parseInt() of the global object. |
Let us try to elaborate on the above Number methods introduced in ES6.
It determines whether the value is Nan or not. It returns true if the value is not a number.
var res = Number.isNaN(NaN); console.log(res); var res1 = Number.isNaN('Nan'); console.log(res1); var res2 = Number.isNaN(123); console.log(res2);
It determines whether a value is a finite number or not. It returns true if the value is of the Number type and equates to a finite number. Otherwise, it returns false.
var res = Number.isFinite(Infinity); console.log(res); var res1 = Number.isFinite(123); console.log(res1); var res2 = Number.isFinite('Infinity'); console.log(res2);
As its name implies, it determines whether the passed value is an integer or not. It returns true if the value is a number and must be an integer (number without decimals). Otherwise, it returns false.
var res = Number.isInteger(-100); console.log(res); var res1 = Number.isInteger(100); console.log(res1); var res2 = Number.isInteger(1.001); console.log(res2);
A safe integer is an integer which is in the range of - (253 - 1) and (253-1). The Number.isSafeInteger() method determines whether or not the value is a safe integer.
var res = Number.isSafeInteger(-100); console.log(res); var res1 = Number.isSafeInteger(100.9); console.log(res1); var res2 = Number.isSafeInteger(-100); console.log(res2); var res3 = Number.isSafeInteger(Math.pow(2,53)); console.log(res3);
ES6 added support for binary literals and changed the way of representing the literals. Let's see the representation of the literals in ES6.
Binary literals in ES6 are represented by using the 0b prefix, followed by the sequence of binary numbers, which are 0 and 1.
The prefix can be written in lowercase as well as in uppercase. However, the lowercase is recommended for the prefix.
console.log(0b010) console.log(0b110) console.log(0b101) console.log(0B100)
Octal literals in ES6 are represented by using the 0o prefix, followed by the sequence of octal digits (from 0 to 7). We cannot include a digit or the combination of digits in octal literal that is out of range (0 to 7).
console.log(0o34) console.log(0o1007) console.log(0o571234)
Hexadecimal literals in ES6 are represented by using the 0x prefix
console.log(0x678) console.log(0x100) console.log(0x788)