JavaScripture
Contribute via GitHub Feedback

Number : Object

A numerical value. JavaScript stores numbers as 64 bit (8 byte) double precision floats. See Math for helpful math related functions. See also BigInt.

Number(value : BigInt) : Number

Converts the specified BigInt to a Number. For values outside the range MIN_SAFE_INTEGER to MAX_SAFE_INTEGER, the returned Number may be an approximation of value.

Example:

Run

Results:

 

Number(value : Object) : Number

Coerces value to a number. Usually this method is not necessary since JavaScript will automatically coerce a value to a number when it is used in a number context. +value is another way to coerce a value to a number. This coercion is very strict and will return NaN if the specified value cannot be converted to a Number. parseInt and paseFloat can also be used to convert a String to a Number.

Example:

Run

Results:

 

Constructors

new Number(value : Number) : Object

Creates a box for value.

Example:

Run

Results:

 

Instance Methods

toExponential([fractionalDigits : Number]) : String

Returns a string representation of this in scientific notation. If fractionalDigits is specified, that many digits will follow the '.'.

Example:

Run

Results:

 

toFixed([fractionalDigits = 0 : Number]) : String

Returns a string representation of this with fractionalDigits following the '.'.

Example:

Run

Results:

 

toLocaleString([locales : Array<String>, [options : Object]]) : String
options : {
currency :String
currencyDisplay :String
localeMatcher :String
maximumFractionDigits :Number
maximumSignificantDigits :Number
minimumFractionDigits :Number
minimumIntegerDigits :Number
minimumSignificantDigits :Number
style :String
useGrouping :Boolean
}

Returns a string representation of this that conforms to the number format specification of the current locale.

Example:

Run

Results:

 

toPrecision(precision : Number) : String

Returns a string representation of this with precision significant digits.

Example:

Run

Results:

 

toString([base = 10 : Number]) : String

Returns a string representation of this in the specified base. base must be an integer between 2 and 36.

Example:

Run

Results:

 

Number Properties

EPSILON : Number  

EPSILON is the difference between 1 and the next Number greater than 1 that is representable in JavaScript.

Example:

Run

Results:

 

MAX_SAFE_INTEGER : Number  

The largest possible integer such that MAX_SAFE_INTEGER and MAX_SAFE_INTEGER + 1 can both be represented exactly in JavaScript. See also MIN_SAFE_INTEGER and Number.isSafeInteger().

Example:

Run

Results:

 

MAX_VALUE : Number  

The largest possible Number less than infinity that can be represented in JavaScript.

Example:

Run

Results:

 

MIN_SAFE_INTEGER : Number  

The smallest possible integer such that MIN_SAFE_INTEGER and MIN_SAFE_INTEGER - 1 can both be represented exactly in JavaScript. See also MAX_SAFE_INTEGER and Number.isSafeInteger().

Example:

Run

Results:

 

MIN_VALUE : Number  

The smallest possible Number greater than 0 that can be represented in JavaScript.

Example:

Run

Results:

 

NaN : Number  

Floating point Not a Number. Signifies an error in a calculation. NaN is never equal to another Number, even if it is NaN. To check if something is NaN, use isNan() or Number.isNan(). Also exists as NaN in the global namespace.

Example:

Run

Results:

 

NEGATIVE_INFINITIY : Number  

Negative infinity.

Example:

Run

Results:

 

POSITIVE_INFINITIY : Number  

Positive infinity. Also exists as Infinity in the global namespace.

Example:

Run

Results:

 

Number Methods

isFinite(x : Number) : Boolean

Returns true if x is not NaN, +Infinity, or -Infinity. See also the global isFinite(x) method.

Example:

Run

Results:

 

isInteger(x : Number) : Boolean

Returns true if x is an integer.

Example:

Run

Results:

 

isNaN(x : Number) : Boolean

Returns true if x is NaN. NaN is never equal to another Number, even if it is NaN, so you must use isNaN to check for NaN. See also the global isNaN(x) method and Object.is().

Example:

Run

Results:

 

isSafeInteger(x : Number) : Boolean

Returns true if x is greater than or equal to MIN_SAFE_INTEGER and less than or equal to MAX_SAFE_INTEGER. Adding or subtracting 1 to numbers outside this range may not produce a change in the value due to lack of floating point precision.

Example:

Run

Results:

 

parseFloat(str : String) : Number

Converts str into a floating point number. parseFloat is less scrict than using Number(str) (or +str) to convert to a Number because it ignores extra characters after the numeric portion of the string. If the first character is not a valid number, parseFloat will return NaN. Also exists as parseFloat in the global namespace.

Example:

Run

Results:

 

parseInt(str : String, [base : Number]) : Number

Converts str into an integral number. If base is not specified, parseInt will attempt to determine the base to use depending on the input ('0x' prefix is base 16). parseInt is less scrict than using Number(str) (or +str) to convert to a Number because it ignores extra characters after the numeric portion of the string. If the first character is not a valid number in the specified base, parseInt will return NaN. Also exists as parseInt in the global namespace.

Example:

Run

Results: