Easy Tutorial
❮ Js Break Jsref Regexp I ❯

JavaScript Number Object


JavaScript has only one type of number.

Numbers can be written with, or without, a decimal point.


JavaScript Numbers

JavaScript numbers can be written with, or without, a decimal point:

Example

Very large or very small numbers can be written with scientific (exponential) notation:

Example


All JavaScript numbers are 64-bit

JavaScript is not a typed language. Unlike many other programming languages, JavaScript does not define different types of numbers, such as integers, short, long, floating-point, etc.

In JavaScript, numbers are not classified into integer and floating-point types; all numbers are represented as floating-point types. JavaScript uses the 64-bit floating-point format defined by the IEEE754 standard to represent numbers, which can represent the maximum value (Number.MAX_VALUE) as **±1.7976931348623157e+308** and the minimum value (Number.MIN_VALUE) as ±5e-324.

This format stores the value in 64 bits, where 0 to 51 bits store the number (fraction/mantissa), 52 to 62 bits store the exponent, and the 63rd bit stores the sign:

Value (Fraction/Mantissa) Exponent Sign
52 bits (0 - 51) 11 bits (52 - 62) 1 bit (63)

Precision

Integers (without a decimal point or exponential notation) are accurate up to 15 digits.

Example

var x = 999999999999999;   // x will be 999999999999999
var y = 9999999999999999;  // y will be 10000000000000000

The maximum number of decimal places is 17, but floating-point arithmetic is not always 100% accurate:

Example

var x = 0.2 + 0.1; // the result will be 0.30000000000000004

Octal and Hexadecimal

If the prefix is 0, JavaScript interprets the numeric constant as an octal number, and if the prefix is 0 and "x", it interprets it as a hexadecimal number.

Example

| | Never write a zero before a number unless you need to perform an octal conversion. | | --- | --- |

By default, JavaScript displays numbers as base 10 decimals.

However, you can use the toString() method to output numbers as base 16, base 8, or base 2.

Example


Infinity

When a numeric operation result exceeds the upper limit of numbers that JavaScript can represent (overflow), the result is a special infinite value, represented in JavaScript as Infinity. Similarly, when the value of a negative number exceeds the range that JavaScript can represent, the result is negative infinity, represented in JavaScript as -Infinity. The behavior of infinite values is consistent with our expectations: addition, subtraction, multiplication, and division based on them still result in infinity (while retaining their positive or negative signs).

Example

myNumber = 2;
while (myNumber != Infinity) {
    myNumber = myNumber * myNumber; // Repeats the calculation until myNumber equals Infinity
}

Dividing by 0 also results in infinity:

Example


NaN - Not-a-Number Value

The NaN property is a special value representing a value that is not a number. This property is used to indicate that a value is not a number. You can set the Number object to this value to indicate that it is not a numeric value.

You can use the isNaN() global function to determine whether a value is NaN.

Example

Dividing by 0 results in infinity, which is a number:

Example


Numbers Can Be Numbers or Objects

Numbers can be initialized with private data, like x = 123;

JavaScript number objects can be initialized with data, like var y = new Number(123);

Example

Example


Number Properties

Property Description
Number.MAX_VALUE Maximum value
Number.MIN_VALUE Minimum value
Number.NaN Not-a-Number
Number.NEGATIVE_INFINITY Negative infinity, returned on overflow
Number.POSITIVE_INFINITY Positive infinity, returned on overflow
Number.EPSILON Represents the difference between 1 and the smallest Number greater than 1
Number.MIN_SAFE_INTEGER Minimum safe integer
Number.MAX_SAFE_INTEGER Maximum safe integer

Number Methods

Method Description
Number.parseFloat() Converts a string to a floating-point number, same as the global method parseFloat()
Number.parseInt() Converts a string to an integer, same as the global method parseInt()
Number.isFinite() Determines whether the passed value is a finite number
Number.isInteger() Determines whether the passed value is an integer
Number.isNaN() Determines whether the passed value is NaN
Number.isSafeInteger() Determines whether the passed value is a safe integer

Methods on the Number Prototype

Method Description
toExponential() Returns a string representing the number in exponential notation, e.g., 1.23e+2
toFixed() Returns a string representing the number with a specified number of decimals, e.g., var a = 123; b = a.toFixed(2); // b = "123.00"
toPrecision() Returns a string representing the number to a specified precision, e.g., var a = 123; b = a.toPrecision(2); // b = "1.2e+2"
❮ Js Break Jsref Regexp I ❯