- ES6 - Discussion
- ES6 - Useful Resources
- ES6 - Quick Guide
- ES9 - New Features
- ES8 - New Features
- ES7 - New Features
- ES6 - Browsers
- ES6 - Image Map
- ES6 - Debugging
- ES6 - Multimedia
- ES6 - Animation
- ES6 - Validations
- ES6 - Proxy API
- ES6 - Reflect API
- ES6 - Object Extensions
- ES6 - Error Handling
- ES6 - Modules
- ES6 - Promises
- ES6 - Maps And Sets
- ES6 - Classes
- ES6 - Collections
- ES6 - Iterator
- ES6 - HTML DOM
- ES6 - RegExp
- ES6 - Math
- ES6 - Date
- ES6 - Arrays
- ES6 - New String Methods
- ES6 - Symbol
- ES6 - Strings
- ES6 - Boolean
- ES6 - Number
- ES6 - Objects
- ES6 - Page Printing
- ES6 - Void Keyword
- ES6 - Dialog Boxes
- ES6 - Page Redirect
- ES6 - Cookies
- ES6 - Events
- ES6 - Functions
- ES6 - Loops
- ES6 - Decision Making
- ES6 - Operators
- ES6 - Variables
- ES6 - Syntax
- ES6 - Environment
- ES6 - Overview
- ES6 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pterals 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 |
The smallest interval between two representable numbers. |
2 |
The maximum safe integer in JavaScript (2^53 - 1). |
3 |
The largest positive representable number. |
4 |
The minimum safe integer in JavaScript (-(2^53 - 1)). |
5 |
The smallest positive representable number - that is, the positive number closest to zero (without actually being zero) |
6 |
Special "not a number" value |
7 |
Special value representing negative infinity; returned on overflow |
8 |
Special value representing infinity; returned on overflow |
9 |
Number.prototype
Special value representing infinity; returned on overflow |
Number Methods
Sr.No | Method & Description |
---|---|
1 |
Determines whether the passed value is NaN. |
2 |
Determines whether the passed value is a finite number. |
3 |
Determines whether the passed value is an integer. |
4 |
Determines whether the passed value is a safe integer (number between -(253 - 1) and 253- 1) |
5 |
The value is the same as parseFloat() of the global object |
6 |
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 | Returns a string representing the number in exponential notation |
2 | Returns a string representing the number in fixed-point notation |
3 |
Returns a string with a language sensitive representation of this number |
4 |
Returns a string representing the number to a specified precision in fixed-point or exponential notation |
5 |
Returns a string representing the specified object in the specified radix (base) |
6 |
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 pterals 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 pteral Extension
ES6 introduces following syntax changes in object pterals declaration.
Object property initiapzer syntax
Computed properties syntax
Concise method syntax
Object property initiapzer
In object property initiapzer syntax, we can initiapze 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 simppfied syntax to include functions in object pterals.
<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