Number in ES6

Number in ES6

In ES6, we have various methods and properties to implement numeric functions such as floating-point, integers, date, etc. The numbers in ES6 enable us to work with number objects. It is the cause of the browser that transforms number literals into number class instance. We can create a number object with the help of the Number constructor(). Sometimes the number object returns NaN. It means the argument can’t be changed into a number.

Syntax

var number = new Number(value);

The above syntax contains the following parameter.

value: It includes the numeric value of an object. If we pass a non-numeric argument in its place, then it returns NaN, because the related argument can’t be a number.

Methods of Number

A number object only consists of the default method, which is part of the object definition. Here, we have the following number methods that are listed below in the tabular form.

            Sr. No.          Method         Description
                1.   Number.isFinite()This method is used to define that the passed number is finite or not.
                2.   Number.isNan()It determines that the passed value is a NaN or not.
                3.   Number.isSafeInteger()This method defines that the passed value is a safe integer (range -253 - 1 and 253- 1) or not.
                4.   Number.isInteger()The Number.isInteger() method defines that the passed value is an integer value or not.
                5.   Number.parseInt()The value of this method is equal to parseInt() of the global object.
                6.   Number.parseFloat()The value of this method is equal to parseFloat() of the global object.

Now, we will elaborate on each method in detail with an example.

Number.isFinite()

This method determines that the value passed is finite or not. It returns true if the value is equal to a finite number. Otherwise, it returns false.

Example

var result = Number.isFinite(Infinity);
console.log(result);
var result1 = Number.isFinite(456);
console.log(result1);
var result2 = Number.isFinite(‘Infinity’);
console.log(result2);

Output

After the execution of the above code, we got the following output:

Number in ES6

Number.isNan()

It is used to define that the passed value is Nan or not. It returns true if the value is a number. Otherwise, it returns false.

Example

Here, we have an example to understand the above method.

var result = Number.isNaN(NaN);
console.log(result);
var result1 = Number.isNaN(‘Nan’);
console.log(result1);
var result2 = Number.isNaN(‘789’);
console.log(result2);

Output

We got the following output after the successful execution of the above code:

Number in ES6

Number.isSafeInteger()

The Number.isSafeInteger() method defines that the passed value is a safe integer or not. The safe integer has the range of -(253 - 1) and (253- 1).

Example

Here, we have an example to understand the above method.

var result = Number.isSafeInteger(-200);
console.log(result);
var result1 = Number.isSafeInteger (100.8);
console.log(result1);
var result2 = Number.isSafeInteger (-200);
console.log(result2);
var result3 = Number.isSafeInteger (Math.pow(2,53));
console.log(result3);

Output

After the successful execution of the following example, we got the following output:

Number in ES6

Number.isInteger()

This defines that the passed value is an integer value or not. This method returns true if the value is an integer. Otherwise, it returns false.

Example

An example is mentioned below to understand the method.

var result = Number.isInteger(-200);
console.log(result);
var result1 = Number.isInteger (200);
console.log(result1);
var result2 = Number.isInteger (1.001));
console.log(result2);

Output                                                                                                      

After the successful execution of the code, we got the following output:

Number in ES6

Properties of Number

The number object contains various properties. These are listed below in the tabular form.

Sr. No.           Property          Description
                   1. Number.EPSILONIt determines the smallest interval between two numbers (presentable).
                   2.Number.MAX_VALUEThis property defines the largest positive representable number.
                   3.Number.MAX_SAFE_INTEGERIt is used to define a maximum safe integer number in JavaScript.
                   4.Number.MIN_VALUEThis property defines the smallest positive number that is closest to zero.
                   5.Number.MIN_SAFE_INTEGERIt is used to define minimum safe integer number in JavaScript.
                   6.Number.NEGATIVE_INFINITYIt determines a number, which is less than the predefined number.
                   7.           Number.NanThis property defines a ‘not a number’ value.
                   8.          Number.prototypeIt defines a special value that determines infinity.
                   9.Number.POSITIVE_INFINITYIt determines a number, which is greater than the predefined number.

Here, we will try to elaborate the object’s property in the detailed form with the help of examples.

EPSILON

The EPSILON property is used to display the difference between 1 and the smallest floating-point number (greater than 1). There is no need to construct a Number object for retrieval of static property because we will use Number.EPSILON property.

Example

We have an example to understand the above property.

var val = Number.EPSILON;
console.log(val);

Output

We got the following output after the execution of the code.

Number in ES6

Number.MAX_VALUE

The Number.MAX_VALUE property refer to a static number object and define constants for the largest positive number.

Example

Here, we have an example for the Number.MAX_VALUE property.

var value = Number.MAX_VALUE;
console.log(“Number.MAX_VALUE is: ” +value);

Output

We have the following output, after the execution of the above code.

Number in ES6

Number.MAX_SAFE_INTEGER

This property is used to define that the passed value is a safe integer or not.

Example

We have an example to understand the above concept.

var value = Number.MAX_SAFE_INTEGER;
console.log(value);

Output

After the successful execution of the code, we got the following output:

Number in ES6

Number.MIN_VALUE

The above property defines the smallest positive number.

Example

Here, we have an illustration for the same.

var value = Number.MIN_VALUE;
console.log(“Number.MIN_VALUE is : + value”);

Output

After the execution of the following output, we got the following output:

Number in ES6

Number.MIN_SAFE_INTEGER

This property is used to determine the minimum safe number in JavScript.

Example

Here, we have an example to understand the above property.

var value = Number.MIN_SAFE_INTEGER;
console.log(“Number.MIN_SAFE_INTEGER is : ” + value);

Output

After the successful execution, we got the following output:

Number in ES6

Representation of Binary Literals

In ES6, the binary literals are represented with the help of prefix 0b, pursued by the binary numbers 1 and 0. We can write prefix in lowercase as well in uppercase.

Example

Here, we have an example for the above property.

console.log(0b010)   
console.log(0b110)   
console.log(0b101)   
console.log(0B100)

Output

After the execution of the above example, we got the following output:

Number in ES6

Representation of octal Literals

The octal literals are represented using prefix 0o, pursued by the octal digit sequence (0 to 7). The combinations of digits or digits are referred as the octal literal.

Example

We have an example to elaborate the above concept.

console.log(0o34)   
console.log(0o1007) 
console.log(0o571234)  

Output

Number in ES6

Representation of Hexadecimal Literals

In ES6, the hexadecimal literals are represented in the form of 0x.

Example

Here, we have an example to understand the above representation.

console.log(0x100)  
console.log(0x678) 
console.log(0x788) 

Output

After the successful execution of the code, we got the following output:

Number in ES6