This tutorial explains what is a number in JavaScript. As you go through it, you'll understand how to write numbers, including JavaScript integers and decimals. You will learn about binary, octal, and hexadecimal numbers, JavaScript infinity and NaN.

The tutorial will cover what JavaScript number precision is as well as number properties. Make sure you fully understand them before learning about number methods.

#### Contents

## JavaScript Number: Main Tips

- There is only
**one type**of number in JavaScript. - Numbers in JavaScript may be written
**with**or**without**decimals. - JavaScript number can hold
**up to 17 digits**after the decimal point until it loses preciseness. - In JavaScript, methods and properties are accessible to numbers, since they are treated
**as objects**when executing.

## Number Format

All numbers in JavaScript are floating point numbers, unlike in other programming languages, which feature multiple types of numbers. Following the international IEEE 754 standard, all Javascript numbers are double precision floating point numbers.

In this format, numbers are stored in 64 bits, and the fraction number is stored in bits 0 to 51. The exponent is stored in bits 52 to 62, and lastly the sign in the bit 63.

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

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

Extremely large or small numbers may be written using **scientific** (exponential) notation. It means that some numbers can be transformed into letters for a shorter and neater looking code.

## Precision

Numbers in JavaScript can be written with or without decimals. It is up to you to decide:

**Example**

```
var a = 21.00; // number with decimals
var b = 12; // number without decimals
```

Although the maximum number of digits held after the decimal point is seventeen, keep in mind that at that point JavaScript numbers are not very precise. They are only accurate up to fifteen digits after the decimal point:

**Example**

```
var a = 999999999999999; // a will be 999999999999999
var b = 9999999999999999; // b will be 10000000000000000
```

Look at the example below to see how floating point arithmetics can not always be 100% accurate when the number of digits after the decimal point exceeds fifteen:

To solve the problem in the editor example above, most developers use multiplying and dividing for precise and neat calculations with JavaScript numbers.

## Hexadecimals

Numeric constants in JavaScript are interpreted as hexadecimals if they are preceded by 0x.

- Numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) are extended to 16 digits.
- Number 9 is preceded by A, B, C, D, E, F.
- A = 10
- B = 11
- C = 12
- D = 13
- E = 14
- F = 15

- Numbers (and letters) are multiplied and the result appears.

This is how it looks in real life:

JavaScript will display numbers as base 10 decimals by default (meaning - 0, 1, 2, 3, 5, 6, 7, 8, 9). However, it is possible to use the `toString()`

method for outputting JavaScript numbers as base 2 (binary), base 8 (octal), or base 16 (hex).

Note:never write JavaScript numbers with a leading zero, since certain JavaScript versions may interpret them as octal (base 8)!

**Pros**

- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features

**Main Features**

- Nanodegree programs
- Suitable for enterprises
- Paid certificates of completion

**Pros**

- Easy to navigate
- No technical issues
- Seems to care about its users

**Main Features**

- Huge variety of courses
- 30-day refund policy
- Free certificates of completion

**Pros**

- Great user experience
- Offers quality content
- Very transparent with their pricing

**Main Features**

- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable

## Infinity

JavaScript `Infinity`

or `-Infinity`

(positive and negative infinity) is the value that is returned by the program when you calculate past the largest possible precise number while using JavaScript.

The largest precise number JavaScript can return is ± 9007199254740991.

**Example**

```
var a = 4;
while (a != Infinity) { // executes until infinity (highest possible number is reached
a = a * a;
}
```

However, not only maximum numbers will generate JavaScript `Infinity`

. Keep in mind that dividing by 0 (zero) will also generate `Infinity`

:

`typeOf Infinity`

returns a JavaScript number definition:

## NaN - Not a Number

If you try to perform arithmetic tasks with non-numeric strings, it will result in `NaN`

(Not a Number), indicating that the selected value is not a number, but rather something else.

However, in case the string does contain a numeric value, the result is still going to be a number.

The global JavaScript `isNaN()`

function can be used to find out whether a value is a number JavaScript can display, or not:

Note:you should look out forNaNJavaScript. UsingNaNin a mathematical operation results in anotherNaN.

Check out an example of using NaN JavaScript in mathematical operations:

The result may also be concatenation (strings being joined end-to-end):

`typeOf(NaN)`

returns a JavaScript number definition:

Note:Keep in mind that string + string =concatenation, while number + number =addition.

## Numbers As Objects

By default JavaScript numbers are all primitive values that are created from literals: `var x = 321`

. However, numbers may also be defined as objects using the keyword `new`

. It looks like this: `var y = new Number(321)`

. This, however, slows down the execution of the script and may produce unwanted results.

When you use the `==`

**loose** equality comparison operator, it performs coercion before comparing the values. It means it tries to change the types of variables to compare the values. Thus, in the example below, the comparison displays `true`

:

When you use `===`

**strict** equality comparison operator, equal numbers are not considered equal if they are not of the same type, e.g. one of the JavaScript numbers is an object and the other is a variable. This is because `===`

checks both type and value:

It must also be noted that JavaScript objects cannot be compared by default.

## Number Properties

Property | Description |
---|---|

MAX_VALUE | Return largest number possible |

MIN_VALUE | Return smallest number possible |

NEGATIVE_INFINITY | Represent negative infinity (returned on overflow) |

NaN | Represent "Not-a-Number" value |

POSITIVE_INFINITY | Represent infinity (returned on overflow) |

The property in the editor example below will return the highest property possible. The same can be done with `MIN_VALUE`

, `NEGATIVE_INFINITY`

, and other properties, as they will also return the specific value according to the property.

Number properties belong to JavaScript number object wrapper that is called `Number`

.

These properties are only accessible as `Number.MAX_VALUE`

.

Using `myNumber.MAX_VALUE`

, where `myNumber`

is a variable, expression, or value, returns `undefined`

.

## JavaScript Number: Summary

- JavaScript number holds the value of numbers in JavaScript. It can be written as hexadecimal.
- After the decimal point, a number JavaScript can hold up to 17 digits, but only up to 15 until it loses precision.
- If the number is too big for a JavaScript program to compile, it will return
`Infinity`

. - If a non-numerical value is inserted in a JavaScript number, the program will return
`NaN`

.