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
var num = new Number(value);

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
snippet
var value = Number.EPSILON;
console.log(value);
Output
2.220446049250313e-16
Number.MAX_SAFE_INTEGER

This property represents the maximum safe integer in JavaScript (253-1).

Example
snippet
var value = Number.MAX_SAFE_INTEGER; 
console.log(value);
Output
9007199254740991
Number.MAX_VALUE

This property belongs to the static Number object and represents constants for the largest possible positive numbers.

Example
snippet
var val = Number.MAX_VALUE; 
console.log("Number.MAX_VALUE equals to: " + val );
Output
Number.MAX_VALUE equals to: 1.7976931348623157e+308
Number.MIN_SAFE_INTEGER

It represents the minimum safe integer in JavaScript (-(253-1)).

Example
snippet
var val = Number.MIN_SAFE_INTEGER; 
console.log("Number. MIN_SAFE_INTEGER equals to: " + val );
Output
Number. MIN_SAFE_INTEGER equals to: -9007199254740991
Number.MIN_VALUE

It represents the smallest positive numeric value.

Example
snippet
var val = Number.MIN_VALUE; 
console.log("Number.MIN_VALUE equals to : " + val );
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
snippet
var res = Number.isNaN(NaN); 
console.log(res); 

var res1 = Number.isNaN('Nan'); 
console.log(res1); 

var res2 = Number.isNaN(123); 
console.log(res2);
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
snippet
var res = Number.isFinite(Infinity); 
console.log(res); 

var res1 = Number.isFinite(123); 
console.log(res1); 

var res2 = Number.isFinite('Infinity'); 
console.log(res2);
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
snippet
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);
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
snippet
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);
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

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
snippet
console.log(0b010) 
console.log(0b110) 
console.log(0b101) 
console.log(0B100)
Output
2 6 5 4
Octal Literals

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
snippet
console.log(0o34) 
console.log(0o1007)
console.log(0o571234)
Output
28 519 193180
Hexadecimal Literals

Hexadecimal literals in ES6 are represented by using the 0x prefix

Example
snippet
console.log(0x678) 
console.log(0x100)
console.log(0x788)
Output
1656 256 1928
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +