# Numbers in JavaScript

## Floats only

Programming languages usually have many different types of numbers, like small integers, integers, floats, doubles, etc. etc. Instead, JavaScript only has one numeric type: 64-bit floating point.

This choice was originally made to make the language easy for beginners, so that you wouldn’t have to worry about what number type to use, but it turned out to be a very fortunate decision. Traditionally, programming languages provide a lot of control for performance reasons, so that if you know you’re using a variable to represent years you use a small number and don’t waste resources, but this is hardly a problem nowadays, as we normally have lots of RAM and CPU cycles to spare. And, of course having only one numeric type is extremely convenient.

64-bit floats means that JavaScript supports numbers up to 9 quadrillion. That is 2^{53}, or 9007199254740992. After that, weird things start to happen. For instance, if you add 1 to it, you get the same number:

9007199254740992+1; // 9007199254740992

There are also two ways to represent 0: -0 and +0. This is because there are two different bit representations for 0, but luckily they are treated the same. SO thread: http://stackoverflow.com/questions/7223359/are-0-and-0-the-same.

## Problems with floats

With floats, the associative law does not hold.

Floating point numbers are implemented with binary fractions, but not precise arithmetics. This means that there will be some very small rounding-off that might create problems in certain situations.

For instance:

0.1 + (0.2 + 0.3); // 0.6

However:

(0.1 + 0.2) + 0.3; // 0.6000000000000001

Which leads to:

(0.1 + 0.2) + 0.3 === 0.1 + (0.2 + 0.3); // false

Although the results should obviously be the same, at the binary level they’re not, so if you compare them you’ll get `false`

.

There is really no solution to this. The safest way to handle this challenge is to know about it, and use integers for operations. For instance, if you’re dealing with money you should use cents, perform your calculations, and then convert the result to $.

## NaN

Another number is `NaN`

, which is a number that doesn’t have a value.

`NaN`

does not equal to anything, including itself. So:

NaN === NaN // false

interestingly:

NaN !== NaN // true

This is kind of wrong, but that’s how it is. Basically, **whenever NaN is used in a comparison, the result will be false**.

We can use this craziness to check if a value is `NaN`

:

isNaN = value !== value;

There is also a more intuitive `isNan`

function: `isNan(value)`

.

Wrong math will return `NaN`

. For instance, the result of dividing 0 by 0 is `NaN`

.

## Numbers are objects + Number function

JavaScript provides a built-in function/constructor object called `Number`

, which is a wrapper object to work with numeric values. If the parameter can’t be converted into a number, the result will be `NaN`

.

var num = new Number(23); // 23 var num = new Number('nope.'); // NaN

Number literals are already object-like. You can call methods on a number literal (parenthesis are necessary to avoid ambiguity between `3.method()`

and `3.3292`

, for instance):

(3).toExponential(); // "3e+0" var x = 4.3832; x.toPrecision(2); // 4.4

When you do that, an object is automatically created in the background, and then discarded after the operation has completed (although JavaScript engines might optimize the actual process and do something else).

Normally one would use number literals, but of course `Number`

can be used as well. One useful property is that if you omit `new,`

the `Number`

function can be used to perform type conversion:

```
var d = new Date('July 30, 1981');
print(Number(d)); // will print 365292000000
```

For a list of all properties and methods of the `Number`

object:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number

## Converting numbers into strings

Strings have the equivalent of the `Number`

function–called `String`

–which will convert what you pass to it into a string. `String`

can be used to convert a number into a string.

`Number`

also has a `toString`

function (inherited from `Function`

), which will do the same thing.

The only difference between the two is that with `toString`

you must be sure the value you’re using is actually a number: `null`

or `undefined`

won’t have a `toString`

function. On the contrary, `String(num)`

will always work, so I guess that would make it the preferred way to do it.

var num = 42; num.toString(); // "42 String(num) // "42"

## Math object

JavaScript also has a `Math`

object.

The reason behind having a separate object is to make JavaScript feel more Java-like (see the history of JavaScript). In reality, a lot of methods you find in `Math`

should have been in `Number`

(like `Math.floor`

, for instance).

Regardless,` Math`

has a lot of important properties and methods.

For a list of all properties and methods of `Math`

:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math