3 things about Infinity in JavaScript you probably already knew, but didn't know why
Tags: javascript, infinity
Published: 2019-12-02 12:50:01

# 1. The largest denominator

If you run the standard `1/0` in Javascript, this yields the Infinity type. But what about very small denominators? If you keep making the denominator smaller, the result will obviously keep getting bigger. But when will it reach Infinity?

```javascript
console.log(1/0);
console.log(1/0.1);
console.log(1/0.000000000000001);
console.log(1/0.0000000000000000000000000000000000000000001);
console.log(1/0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
console.log(1/0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
console.log(1/0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
console.log(1/0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
console.log(1/0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
console.log(1/0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
console.log(1/0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
```
Output:
```
Infinity
10
999999999999999.9
9.999999999999999e+42
1e+85
1.0000000000000001e+304
1e+305
1e+306
1.0000000000000001e+307
1e+308
Infinity
```

So as we reach the 309th decimal place, we get Infinity. This might not be a novel revelation, because as we all may know (actually TIL) that *1.797693134862315E+308 is the limit of a floating point number*. This is also a roundabout way of asking *When does a number turn into Infinity in Javascript?*. As you would expect:
```javascript
console.log(1e306); // 1e+306
console.log(1e307); // 1e+307
console.log(1e308); // 1e+308
console.log(1e309); // Infinity
```

# 2. Parsing Infinity
Sometimes we don't always get what we want when we parse stuff. Infinity at the least is no different. Observe:
```javascript
parseInt(Infinity); // NaN
parseFloat(Infinity); // Infinity
```
Tada! Okay but the first line can be explained pretty easily. `parseInt` is expecting a string type parameter, and if the argument cannot be converted into a ninteger, it returns `NaN`. So this makes sense since `Infinity` is not within Javascript's integer range.

However, as we've seen in the previous point, Infinity is actually represented by the `float` type in Javascript. Hence the `parseFloat` result makes sense!

Also, a bonus: 
```javascript
//
parseInt("Infinity", 10); // -> NaN
// ...
parseInt("Infinity", 18); // -> NaN...
parseInt("Infinity", 19); // -> 18
// ...
parseInt("Infinity", 23); // -> 18...
parseInt("Infinity", 24); // -> 151176378
// ...
parseInt("Infinity", 29); // -> 385849803
parseInt("Infinity", 30); // -> 13693557269
// ...
parseInt("Infinity", 34); // -> 28872273981
parseInt("Infinity", 35); // -> 1201203301724
parseInt("Infinity", 36); // -> 1461559270678...
parseInt("Infinity", 37); // -> NaN
```

I got that from this [fun repo](https://github.com/denysdovhan/wtfjs#parseint-is-a-bad-guy)

# 3. Infinity is not a number, right?
Okay so without getting too much into Number Theory (I'm not even bragging, I have no idea about Number Theory), let's see how Javascript deals with Infinity as a type.
```javascript
typeof(Infinity) // "number"
```

Okay, so it's a number. But is it?

```javascript
1 + 1 // 2
Infinity + Infinity // Infinity
5 - 5 // 0
Infinity - Infinity // NaN
```

Wait, what? Okay speaking in Javascript terms, when you write `Infinity - Infinity`, it is evaluated as this:

```javascript
Infinity + (-Infinity) // NaN
```

Since `Infinity` and `-Infinity` are different 'objects' in Javascript, this would yield a `NaN`. So wait, it's not a number? or is it? I actually don't know this one.

Thanks for reading!