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.
The prefix can be written in upper or lower case. However, it is suggested to stick to the lowercase version.
Example − Binary Representation
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 4Example − Octal Representation
console.log(0o010) console.log(0o100)The following output is displayed on successful execution of the above code.
8 64Example − Hexadecimal Representation
console.log(0o010) console.log(0o100)The following output is displayed on successful execution of the above code.
255 384
No comments:
Post a Comment