Number in ES6

Facebooktwitterredditpinterestlinkedinmailby feather

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

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

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.

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.

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.

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.EPSILON It determines the smallest interval between two numbers (presentable).
                   2. Number.MAX_VALUE This property defines the largest positive representable number.
                   3. Number.MAX_SAFE_INTEGER It is used to define a maximum safe integer number in JavaScript.
                   4. Number.MIN_VALUE This property defines the smallest positive number that is closest to zero.
                   5. Number.MIN_SAFE_INTEGER It is used to define minimum safe integer number in JavaScript.
                   6. Number.NEGATIVE_INFINITY It determines a number, which is less than the predefined number.
                   7.            Number.Nan This property defines a ‘not a number’ value.
                   8.           Number.prototype It defines a special value that determines infinity.
                   9. Number.POSITIVE_INFINITY It 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.

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.

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.

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.

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.

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.

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.

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.

Output

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

Number in ES6
Facebooktwitterredditpinterestlinkedinmailby feather