JavaScript Numbers
Tags: beginners, javascript
Published: 2019-12-28 00:44:36

---
title: JavaScript Numbers
published: true
description: A beginners introduction to numbers in JavaScript
tags: beginners,javascript
series: FrontEnd Development Zero to Hero
---

Programmer or not, am pretty sure you've encountered numbers before. I mean, numbers are everywhere from _economics_ to _physics_ and even in nature.

It is quiet easy to get started with numbers in JavaScript but, before long you'll come to the fact that __numbers in JavaScript can be really weird__ . If you are familiar with the behavior of numbers in some programming languages this won't come as a surprise to you.

_All screenshots are from <b>Firefox 71.0</b> and its Developer Tools. One particular feature in Firefox 71.0 that's worthy of mention is the <b>multi-line code editor</b> in the console._

<hr>

Every _computation_ performed by a computer program will involve numbers directly or indirectly. Directly, if you are doing some calculations and indirectly, because __all high level programming code are converted to machine code__ which are numbers (`0's` and `1's`).

You might not deal with number's often but it is an huge advantage if you know how they work in JavaScript most especially _floating point numbers_.

JavaScript represents numbers using the _64-bit floating-point format_ defined by the [IEEE 754 standard](https://en.wikipedia.org/wiki/IEEE_754). Which means it can represent numbers as large as <code>±1.7976931348623157 × 10<sup>308</sup></code> and as small as <code>±5 × 10<sup>−324</sup></code>.

When a number appears directly in a JavaScript program, it’s called a _numeric literal_.

Just like regular math, you can perform calculations in JavaScript using popular math operators like `+`, `-` , `/` and `%` (modulus).

Lets play with some code.

```javascript
// addition
console.log(1 + 1);

// multiplication
console.log(2 * 3);

// division
console.log(4 / 2);

// modulus or remainder
console.log(2 % 2);

// exponential operator introduced in
// EcmaScript 2016 (ES7)
console.log(2 ** 2);

// exponential calculation using the Math.pow()
// function, this is similar to the ** (exponential operator)
// introduced in EcmaScript 2016 (ES7)
console.log(Math.pow(2,2));
```

The code when executed in the console:

![Numbers in JavaScript](https://thepracticaldev.s3.amazonaws.com/i/q6f83mvnrw6rr4x4vctg.PNG)

The code examples above are example of _numeric literals_.

Now, let's  have a look at _floating point numbers_.

```javascript
/**
  * Basic calculation with floating point numbers
  */

// addition
console.log(0.1 + 1.2);

// multiplication
console.log(2.2 * 3.12);

// division
console.log(4.1 / 2.08);

// modulus or remainder
console.log(2.0 % 1.2);

// exponential operator introduced in
// EcmaScript 2016 (ES7)
console.log(2.2 ** 2);

// exponential calculation using the Math.pow()
// function, this is similar to the ** (exponential operator)
// introduced in EcmaScript 2016 (ES7)
console.log(Math.pow(0.2,2));
```

The code when executed in the console:

![Numbers in JavaScript](https://thepracticaldev.s3.amazonaws.com/i/euhlnpj1foioetg99cjp.PNG)

I mentioned earlier that JavaScript numbers are weird, well it has to do with calculations and comparisons involving _floating point numbers_. Take a look at the following example.

```javascript
0.1 + 0.1 == 0.2;

// Expected output: true
```

When executed in the console:

![Numbers in JavaScript](https://thepracticaldev.s3.amazonaws.com/i/sz1ihk1j9ditd2p2m27s.PNG)

The next calculation might surprise you.

```javascript
0.1 + 0.2 == 0.3

// Expected output: ?
```

When executed in the console:

![Numbers in JavaScript](https://thepracticaldev.s3.amazonaws.com/i/f7vz7hd7v6kwnmi8plrp.PNG)

Why is this? Well that's because numbers in JavaScript are defined by the [IEEE 754 standard](https://en.wikipedia.org/wiki/IEEE_754).

The last code execution evaluated to false because `0.1 + 0.2` is actually `0.30000000000000004` not `0.3`. You can confirm this in the console.

![Numbers in JavaScript](https://thepracticaldev.s3.amazonaws.com/i/wrdnnlrhcsxp5yh3kykc.PNG)

Yeah, i know this is weird. There is even a website dedicated to this specific number `0.30000000000000004`. The website is https://0.30000000000000004.com.

You can visit the site for more information about floating point numbers.

Numbers can be made negative by adding a negative sign (`-`) before the number.

![Negative numbers in JavaScript](https://thepracticaldev.s3.amazonaws.com/i/h53lqokx6qhllepslghl.PNG)

__Division by zero is not an error in JavaScript__ it simply returns `Infinity` or negative infinity (`-Infinity`).

```javascript
2/0

// Expected output: infinity
```

When executed in the console: 

![Numbers in JavaScript](https://thepracticaldev.s3.amazonaws.com/i/385cgeybbf3q248jsets.PNG)

There is one exception however, __zero divided by zero does not have a well-defined value__ and the result of this operation is the special not-a-number value, printed as `NaN`.

```javascript
0/0

// Expected output: NaN
```

When executed in the console:

![Numbers in JavaScript](https://thepracticaldev.s3.amazonaws.com/i/8rff0znln7dbn4h6fbiz.PNG)

`NaN` __also arises if you attempt to divide__ `Infinity` by `Infinity` or __take the square root of a negative number or use arithmetic operators with non-numeric operands that cannot be converted to numbers__.

There are also _hexadecimal numbers_ and _octal numbers_ in JavaScript. They are not included in this post for the sake of simplicity.

Up next, Loops.