ES6 – Number


ES6 – Number



”;


The Number object represents numerical date, either integers or floating-point numbers. In general, you do not need to worry about Number objects because the browser automatically converts number literals to instances of the number class.

Following is the syntax for creating a number object.


var val = new Number(number); 

In the place of number, if you provide any non-number argument, then the argument cannot be converted into a number, it returns NaN (Not-a-Number).

Number Properties












Sr.No Property & Description
1
Number.EPSILON

The smallest interval between two representable numbers.

2
Number.MAX_SAFE_INTEGER

The maximum safe integer in JavaScript (2^53 – 1).

3
Number.MAX_VALUE

The largest positive representable number.

4
MIN_SAFE_INTEGER

The minimum safe integer in JavaScript (-(2^53 – 1)).

5
Number.MIN_VALUE

The smallest positive representable number – that is, the positive number closest to zero (without actually being zero)

6
Number.Nan

Special “not a number” value

7
Number.NEGATIVE_INFINITY

Special value representing negative infinity; returned on overflow

8
Number.POSITIVE_INFINITY

Special value representing infinity; returned on overflow

9
Number.prototype

Special value representing infinity; returned on overflow

Number Methods









Sr.No Method & Description
1
Number.isNaN()

Determines whether the passed value is NaN.

2
Number.isFinite()

Determines whether the passed value is a finite number.

3
Number.isInteger()

Determines whether the passed value is an integer.

4
Number.isSafeInteger()

Determines whether the passed value is a safe integer (number between -(253 – 1) and 253- 1)

5
Number.parseFloat()

The value is the same as parseFloat() of the global object

6
Number.parseInt()

The value is the same as parseInt() of the global object

Number Instances Methods

The Number object contains only the default methods that are a part of every object”s definition.









Sr.No Instance Method & Description
1 toExponential()

Returns a string representing the number in exponential notation

2 toFixed()

Returns a string representing the number in fixed-point notation

3
toLocaleString()

Returns a string with a language sensitive representation of this number

4
toPrecision()

Returns a string representing the number to a specified precision in fixed-point or exponential notation

5
toString()

Returns a string representing the specified object in the specified radix (base)

6
valueOf()

Returns the primitive value of the specified object.

Binary and Octal Literals

Before ES6, your best bet when it comes to binary or octal representation of integers was to just pass them to parseInt() with the radix. In ES6, you could use the 0b and 0o prefix to represent binary and octal integer literals respectively. Similarly, to represent a hexadecimal value, use the 0x prefix.

The prefix can be written in upper or lower case. However, it is suggested to stick to the lowercase version.

Example − Binary Representation


console.log(0b001) 
console.log(0b010) 
console.log(0b011) 
console.log(0b100)

The following output is displayed on successful execution of the above code.


1 
2 
3 
4

Example − Octal Representation


console.log(0o010)
console.log(0o100)

The following output is displayed on successful execution of the above code.


8
64

Example − Hexadecimal Representation


console.log(0x010)
console.log(0x100)

The following output is displayed on successful execution of the above code.


255
384

Object literal Extension

ES6 introduces following syntax changes in object literals declaration.

  • Object property initializer syntax
  • Computed properties syntax
  • Concise method syntax

Object property initializer

In object property initializer syntax, we can initialize an object directly with variables. This will create attributes which have same name as that of the variables.


<script>
   let firstName = ''Tutorials'',lastName=''Point''
   let company = {
      firstName,
      lastName
   }
   console.log(company)
   console.log(company.firstName)
   console.log(company.lastName)
</script>

The output of the above code will be as given below −


{firstName: "Tutorials", lastName: "Point"}
Tutorials
Point

Computed Properties

In computed properties syntax the property of object can be dynamically created from variables. In the following example, a variable by the name suffix is used to compute the company object.


<script>
   let suffix = ''Name''
   let company = {
      [''first''+suffix]:''Tutorials'',
      [''last''+suffix]:''Point''
   }
   console.log(company)
   console.log(company[''firstName''])
   console.log(company[''lastName''])
</script>

The output of the above code will be as shown below −


{firstName: "Tutorials", lastName: "Point"}
Tutorials
Point

In Concise method syntax we can use and declare a method directly without the use of function keyword. This is a simplified syntax to include functions in object literals.


<script>
   let firstName = ''Tutorials'',lastName=''Point''
   let company = {
      firstName,
      lastName,
      getFullName(){
         return this.firstName+" - "+this.lastName
      }
   }
   console.log(company.getFullName())
   console.log(company)
</script>

The output of the above code will be as mentioned below −


Tutorials - Point
{firstName: "Tutorials", lastName: "Point", getFullName: ƒ}

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *