# JavaScript Numbers

JavaScript has only one number type, there is no separate designation for integers and floating-point numbers.

Because of this, numbers can be written in JavaScript with or without decimals.

In both cases above, the data type is a number and is the same regardless of whether or not the number has decimal points.

Scientific exponential notation can be used in JavaScript to abbreviate very large or small numbers, as in the following example:

Numbers in JavaScript are considered to be accurate up to 15 digits. That means that numbers will be rounded after the 16th digit is reached:

## The Internal Representation of Numbers

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

JavaScript numbers have 64-bit precision, which is also called double precision.

The internal representation is based on the IEEE 754 standard.

The 64 bits are distributed between a number’s sign, exponent, and fraction as follows:

Sign Exponent Fraction
1 bit 11 bits 52 bits
Bit 63 Bits 62–52 Bits 51–0

As you know from the previous chapters, the `+` operator is used for both addition and concatenation.

Numbers are added. Strings are concatenated.

If you add two numbers, the result will be a number:

If you add two strings, the result will be a string concatenation:

If you add a number and a string, the result will be a string concatenation:

If you add a string and a number, the result will be a string concatenation:

JavaScript evaluates expressions from left to right. Different sequences can produce different results.

In the below example, first 50 + 10 is added because x and y are both numbers, then 60 + "30" is concatenated because z is a string:

A common mistake is to expect this result to be 60:

You can solve above problem by using parentheses:

## Numeric Strings and Mathematical Expressions

JavaScript strings can contain numeric values.

If a string contains a numeric value, the mathematical expression can be performed in JavaScript.

Division can be performed:

Multiplication can be performed:

Exponentiation can be performed:

Incrementation can be performed:

Subtraction can be performed:

Note: If you add two strings, the result will be a string concatenation.

## JavaScript NaN - Not a Number

In JavaScript `NaN` is a reserved word indicating that a number is not a legal number.

If you attempt to perform a mathematical operation on a number and a non-numeric value, `NaN` will be returned.

However, if a string contains a numeric value, the mathematical expression can be performed in JavaScript:

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

When assigning the value `NaN` to a variable used in an operation, it will result in the value of `NaN`, even when the other operand is a legal number:

`NaN` is a number; `typeof NaN` returns `number`:

## JavaScript Infinity

`Infinity` or `-Infinity` will be returned if you calculate a number outside of the largest possible number available in JavaScript.

These will also occur for values that are undefined, as when dividing by zero:

In technical terms, `Infinity` will be displayed when a number exceeds the number `1.797693134862315E+308`, which represents the upper limit in JavaScript.

Similarly, `-Infinity` will be displayed when a number goes beyond the lower limit of `-1.797693134862316E+308`.

`Infinity` is a number: `typeof Infinity` returns `number`:

The number `Infinity` can also be used in loops:

## JavaScript Number Base

By default, JavaScript displays numbers as base 10 decimals.

Numbers can also be represented in hexadecimal (base 16), binary (base 2), and octal (base 8) notation.

Hexadecimal numbers are prefixed with `0x`:

Binary numbers are prefixed with `0b`:

Never write a number with a leading zero (like 0121). JavaScript interpret numbers as octal if they are written with a leading zero:

You can use the `toString()` method to return numbers from base 2 to base 36.

Hexadecimal is base 16, Decimal is base 10, Octal is base 8, Binary is base 2.

## Number Primitives and Number Objects

Normally, JavaScript numbers are primitive values, created from literals:

var num = 50;

But numbers can also be defined as objects using the ` new` keyword:

var num = new Number(50);

In order to test the difference between the two, we will initialize a number primitive and a number object.

Note: Don't create numbers as objects. It slows down execution speed and can produce some unexpected results.

When using the `==` operator, equal numbers are equal:

When using the `===` operator, equal numbers are not equal, because the `===` operator expects equality in both value and type:

Objects cannot be compared:

Notice the difference between (`==`) and (`===`). Comparing two JavaScript objects will always return false.