ES6 Numbers
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.
Syntax
Parameter
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.
Number Properties
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.
EPSILON
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.
Example
Output
2.220446049250313e-16
Number.MAX_SAFE_INTEGER
This property represents the maximum safe integer in JavaScript (253-1).
Example
Output
9007199254740991
Number.MAX_VALUE
This property belongs to the static Number object and represents constants for the largest possible positive numbers.
Example
Output
Number.MAX_VALUE equals to: 1.7976931348623157e+308
Number.MIN_SAFE_INTEGER
It represents the minimum safe integer in JavaScript (-(253-1)).
Example
Output
Number. MIN_SAFE_INTEGER equals to: -9007199254740991
Number.MIN_VALUE
It represents the smallest positive numeric value.
Example
Output
Number.MIN_VALUE equals to : 5e-324
Number Methods
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.
Number.isNan()
It determines whether the value is Nan or not. It returns true if the value is not a number.
Example
Output
true false false
Number.isFinite()
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.
Example
Output
false true false
Number.isInteger()
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.
Example
Output
true true false
Number.isSafeInteger()
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.
Example
Output
true false true true
Binary, Octal and Hexadecimal Literals
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 representation
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.
Example
Output
2 6 5 4
Octal Literals representation
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).
Example
Output
28 519 193180
Hexadecimal Literals representation
Hexadecimal literals in ES6 are represented by using the 0x prefix
Example
Output
1656 256 1928