JavaScript Numbers


In JavaScript, there's only one kind of number. You can write numbers with or without decimals.

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


JavaScript Numbers are Always 64-bit Floating Point

JavaScript is unique among programming languages because it doesn't categorize numbers into various types, such as integers, short numbers, long numbers, or floating-point numbers.

In JavaScript, numbers are saved as double precision floating point numbers, using a worldwide standard known as IEEE 754. This means that numbers are stored using 64 bits, with the fraction 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)

Integer Precision

Whole numbers (numbers without a dot or special notation) are correct up to 15 digits.

The maximum number of decimals is 17.


Floating Precision


Adding Numbers and Strings

WARNING !!

JavaScript employs the + sign for addition and joining strings.

Numbers are added. Strings are concatenated.

When you combine two numbers, you get another number.

When you combine two pieces of text, you get a longer piece of text.

When you add a number and a string, the outcome is a combination of both as a string.

Adding a string and a number also results in a string.

One common error is anticipating the outcome to be 30.

A typical error is assuming the outcome will be 102030:

The JavaScript interpreter works from left to right.

To begin, add 10 and 20 together because x and y are both numbers.

When you add 30 to the string "30," it becomes a combined string because "z" is a text.


Numeric Strings

JavaScript strings can have numeric content:

let x = 100;         // x is a number

let y = "100";       // y is a string

In JavaScript, whenever you perform a mathematical operation, the language will attempt to change strings into numbers.

This will work:

This will also work:

And this will work:

But this will not work:

In the last example JavaScript uses the + operator to concatenate the strings.


NaN - Not a Number

NaN is a special word in JavaScript. It tells us that a number is not a valid number.

Doing math with something that's not a number will give you a result of NaN (which means Not a Number):

But if the text consists of numbers, the outcome will also be a number:

You can check if a value is not a number in JavaScript by using the global function called isNaN().

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

Or the result might be a concatenation like NaN5:

NaN is a number: when we check its type using typeof NaN, it tells us that NaN is of the number type.


Infinity

Infinity is what JavaScript gives you when you try to work with a number that's beyond the biggest number it can handle.

Division by 0 (zero) also generates Infinity:

Infinity is like a number. When we check the type of Infinity, it tells us that it is of the type number.


Hexadecimal

JavaScript reads numeric constants as hexadecimal if they start with "0x".

Don't use a leading zero in numbers (e.g., 07).
Some JavaScript versions may read numbers as octal when they start with a zero.

Usually, JavaScript shows numbers in their regular decimal form, which is also known as base 10 decimals.

You can employ the toString() method to display numbers in various bases, from base 2 to base 36.

Hexadecimal is like counting to 16. Decimal is like counting to 10. Octal is like counting to 8. Binary is like counting to 2.


JavaScript Numbers as Objects

Normally JavaScript numbers are primitive values created from literals:

let x = 123;

Numbers can also be described as things using the new keyword.

let y = new Number(123);

Do not create Number objects.

The new keyword makes the code harder to understand and reduces performance speed.

Number Objects can produce unexpected results:

Note the difference between (x==y) and (x===y).

Comparing two JavaScript objects always returns false.