# JavaScript Numbers

JavaScript has only one type of number.

Numbers can be written with, or without decimals.

## JavaScript Numbers

JavaScript numbers can be written with, or without decimals:

## Example

var y = 34; // A number without decimals

Extra large or extra small numbers can be written with scientific (exponent) notation:

## Example

var y = 123e-5; // 0.00123

## JavaScript Numbers are Always 64-bit Floating Point

Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.

JavaScript numbers are always stored as double precision floating point
numbers, following the international IEEE 754 standard.

This format
stores numbers in 64 bits, where the number (the fraction) is stored in bits 0
to 51, the exponent in bits 52 to 62, and the sign in bit 63:

Value (aka Fraction/Mantissa) | Exponent | Sign |
---|---|---|

52 bits (0 - 51) | 11 bits (52 - 62) | 1 bit (63) |

## Precision

Integers (numbers without a period or exponent notation) are considered accurate up to 15 digits.

## Example

var y = 9999999999999999; // y will be 100000000000000

Try it yourself »

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

To solve the problem above, it helps to multiply and divide:

## Hexadecimal and Octal

JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.

Never write a number with a leading zero. Some JavaScript versions interprets numbers as octal if they are written with a leading zero. |

By default, Javascript displays numbers as base 10 decimals.

But you can use the toString() method to output numbers as base 16 (hex), base 8 (octal), or base 2 (binary).

## Example

myNumber.toString(16); // returns 80

myNumber.toString(8); // returns 200

myNumber.toString(2); // returns 10000000

Try it yourself »

## Infinity

Infinity (or -Infinity) is the value JavaScript will return if you calculate a number outside the largest possible number.

## Example

while (myNumber != Infinity) {

myNumber = myNumber * myNumber; // Calculate myNumber until Infinity

}

Try it yourself »

Division by 0 (zero) also generates Infinity:

## Example

var y = -2 / 0; // y will be -Infinity

Try it yourself »

Infinity is a number: typeOf(Infinity) is a Number.

## NaN - Not a Number

NaN is a JavaScript reserved word indicating that a value is not a number.

You can use the global JavaScript function isNaN() to find out if a value is a number.

## Example

var y = 100 / "10"; // a number divided by a numeric string is a number

Try it yourself »

Infinity is a number.

Watch out for NaN. If you use it in a mathematical operation, the result will also be NaN.

## Numbers Can be Numbers or Objects

JavaScript numbers can be primitive values created from literals, like var x = 123;

JavaScript number can also be objects created with the new keyword, like var y = new Number(123);

## Example

var y = new Number(123);

typeof(x) // returns number

typeof(y) // returns object

Try it yourself »

Normally, because of some nasty side effects, you will not define numbers as objects.

## Example

var y = new Number(123);

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

Try it yourself »

Primitive values, like 3.14, cannot have properties and methods (because they are not objects). However, JavaScript treats primitive values as objects when accessing properties and methods. |

## Number Properties

- MAX_VALUE
- MIN_VALUE
- NEGATIVE_INFINITY
- POSITIVE_INFINITY
- NaN
- prototype
- constructor

All number properties are properties of JavaScript's number object wrapper
called **Number**.

These properties can only be accessed as **Number**.MAX_VALUE.

Using **num**.MAX_VALUE, where **num** is an object or a primitive number value, will return
undefined.

## Number Methods

- toExponential()
- toFixed()
- toPrecision()
- toString()
- valueOf()

## Complete Number Object Reference

For a complete reference of all the properties and methods that can be used with the Number object, go to our Complete Number Object Reference.

The reference contains both descriptions and examples, for each property and method.

## Your suggestion:

## Thank You For Helping Us!

Your message has been sent to W3Schools.