Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Number object

JavaScript Number object


JavaScript has only one number type.

Numbers can be written with or without a decimal point.


JavaScript numbers

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

Example

var pi=3.14; // with a decimal point

var x=34; // without a decimal point

Extremely large or small numbers can be written using scientific (exponential) notation:

Example

var y=123e5;    // 12300000

var z=123e-5;   // 0.00123


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 integer, short, long, floating point, and so on.

In JavaScript, numbers are not divided into integer and floating-point types; all numbers are floating-point. JavaScript uses the 64-bit floating-point format defined by the IEEE 754 standard to represent numbers. The maximum value it can represent (Number.MAX_VALUE) is ±1.7976931348623157e+308, and the minimum value (Number.MIN_VALUE) is ±5e-324。

This format uses 64 bits to store a number: bits 0 to 51 store the digits (fraction), bits 52 to 62 store the exponent, and bit 63 stores the sign:

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


Precision

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

Example

var

x

=

999999999999999

;

//

x is 999999999999999

var

y

=

9999999999999999

;

//

y is 10000000000000000

Try it »

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

Example

var x = 0.2+0.1; // result is 0.30000000000000004

Try it »


Octal and hexadecimal

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

Example

var y = 0377;

var z = 0xFF;

Try it »

lamp Never write a leading zero in front of a number unless you need octal conversion. 

By default, JavaScript numbers are displayed in decimal.

But you can use the toString() method to output hexadecimal, octal, and binary.

Example

var myNumber=128;

myNumber.toString(16); // returns 80

myNumber.toString(8); // returns 200

myNumber.toString(2); // returns 10000000

Try it »


Infinity

When the result of a numeric operation exceeds the maximum number JavaScript can represent (overflow), the result is a special infinity value, represented in JavaScript as Infinity. Likewise, when a negative value exceeds the range of negative numbers JavaScript can represent, the result is negative infinity, represented as -Infinity. Infinity values behave as expected: addition, subtraction, multiplication, and division involving them still yield infinity (of course, keeping their sign).

Example

myNumber=2;

while (myNumber!=Infinity)

{

    myNumber=myNumber*myNumber; // repeat until myNumber equals Infinity

}

Try it »

Dividing by 0 also produces infinity:

Example

var x = 2/0;

var y = -2/0;

Try it »


NaN - Not a Number

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

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

Example

var x = 1000 / "Apple";

isNaN(x); // returns true

var y = 100 / "1000";

isNaN(y); // returns false

Try it »

Dividing by 0 gives infinity, and infinity is a number:

Example

var x = 1000 / 0;

isNaN(x); // returns false

Try it »


A number can be a number or an object

A number can be initialized with primitive data, like x = 123;

A JavaScript Number object initializes data with var y = new Number(123);

Example

var x = 123;

var y = new Number(123);

typeof(x) // returns Number

typeof(y) // returns Object

Try it »

Example

var x = 123;             

var y = new Number(123);

(x === y) // is false, because x is a number and y is an object

Try it »


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

The difference between 1 and the smallest Number greater than 1

Number.MIN_SAFE_INTEGER The smallest safe integer.
Number.MAX_SAFE_INTEGER The largest safe integer.

Number methods

Method Description
Number.parseFloat() Converts a string to a floating-point number, same as the global method parseFloat() in its effect.
Number.parseInt()

Converts a string to an integer, same as the global method parseInt() in its effect.

Number.isFinite() Determines whether the passed argument is a finite number.
Number.isInteger() Determines whether the passed argument is an integer.
Number.isNaN() Determines whether the passed argument is isNaN().
Number.isSafeInteger() Determines whether the passed argument is a safe integer.

Some methods on the Number prototype

Method Description
toExponential() Returns a string of a number in exponential form, e.g., 1.23e+2
toFixed()

Returns a representation with the specified number of decimal places.

var a=123; b=a.toFixed(2); // b="123.00" | | toPrecision() |

Returns a number with the specified precision. In the example below, with a=123, the 3 disappears due to precision limits:

var a=123;

b=a.toPrecision(2); // b="1.2e+2"
|