JavaScript From Beginner To Advanced

0% completed

Previous
Next
JavaScript - Number

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 in NaN.

Properties

The Number object comes with several built-in properties:

No.PropertyDescription
1MAX_VALUERepresents the largest positive numeric value representable in JavaScript.
2MIN_VALUERepresents the smallest positive numeric value representable in JavaScript.
3NaNRepresents "Not-a-Number" value.
4NEGATIVE_INFINITYRepresents negative infinity; returned on overflow.
5POSITIVE_INFINITYRepresents positive infinity; returned on overflow.
6EPSILONRepresents the difference between 1 and the smallest floating point number greater than 1.
7MAX_SAFE_INTEGERRepresents the maximum safe integer in JavaScript (2^53 - 1).
8MIN_SAFE_INTEGERRepresents 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 NameDescription
1toFixed()Converts a number into a string, rounding to a specified number of decimals.
2toPrecision()Converts a number into a string with a specified length.
3toString()Converts a Number object to a string in a specified base.
4valueOf()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 NameDescription
1isFinite()Determines whether the passed value is a finite number.
2isInteger()Determines whether the passed value is an integer.
3isNaN()Determines whether the passed value is NaN.
4isSafeInteger()Determines whether the provided value is a safe integer.
5parseFloat()Parses an argument and returns a floating point number.
6parseInt()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:

Javascript
Javascript

. . . .
  • Displays the largest positive numeric value representable in JavaScript.

Instance Method: toFixed()

Javascript
Javascript

. . . .
  • toFixed(2) formats the num instance to a string with 2 decimal places.

Static Method: parseInt()

Javascript
Javascript

. . . .
  • parseInt() parses the string str and returns the integer part.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next