0% completed
The Number object in JavaScript is a wrapper object allowing you to work with numerical values. It provides properties and methods for mathematical operations, enabling you to perform computations and manage numeric data effectively.
Syntax
The Number object in JavaScript can be instantiated using the new
keyword, followed by the Number()
constructor. The new
keyword creates a new instance of the Number object, wrapping the numeric value provided as an argument to the constructor.
var numInstance = new Number(value);
value
: The numeric value you want to represent as a Number object. If the value cannot be converted into a number, it results inNaN
.
Properties
The Number object comes with several built-in properties:
No. | Property | Description |
---|---|---|
1 | MAX_VALUE | Represents the largest positive numeric value representable in JavaScript. |
2 | MIN_VALUE | Represents the smallest positive numeric value representable in JavaScript. |
3 | NaN | Represents "Not-a-Number" value. |
4 | NEGATIVE_INFINITY | Represents negative infinity; returned on overflow. |
5 | POSITIVE_INFINITY | Represents positive infinity; returned on overflow. |
6 | EPSILON | Represents the difference between 1 and the smallest floating point number greater than 1. |
7 | MAX_SAFE_INTEGER | Represents the maximum safe integer in JavaScript (2^53 - 1 ). |
8 | MIN_SAFE_INTEGER | Represents the minimum safe integer in JavaScript (-(2^53 - 1) ). |
Number Instance Methods
Instance methods are called on instances of the Number object. Here are some notable instance methods:
No. | Method Name | Description |
---|---|---|
1 | toFixed() | Converts a number into a string, rounding to a specified number of decimals. |
2 | toPrecision() | Converts a number into a string with a specified length. |
3 | toString() | Converts a Number object to a string in a specified base. |
4 | valueOf() | Returns the primitive value of a Number object. |
Number Static Methods
Static methods are called on the Number class itself, not on instances of the Number class.
No. | Method Name | Description |
---|---|---|
1 | isFinite() | Determines whether the passed value is a finite number. |
2 | isInteger() | Determines whether the passed value is an integer. |
3 | isNaN() | Determines whether the passed value is NaN. |
4 | isSafeInteger() | Determines whether the provided value is a safe integer. |
5 | parseFloat() | Parses an argument and returns a floating point number. |
6 | parseInt() | Parses a string argument and returns an integer of the specified radix. |
Examples
Let's illustrate the usage of some instance and static methods:
MAX_VALUE
Property:
- Displays the largest positive numeric value representable in JavaScript.
Instance Method: toFixed()
toFixed(2)
formats thenum
instance to a string with 2 decimal places.
Static Method: parseInt()
parseInt()
parses the stringstr
and returns the integer part.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible