twitch
Streaming on Twitch twice a week

Math with Infinity

JavaScript

Calculations with Infinity are pretty rare, and most of them still return Infinity. While Infinity is a number, trying to subtract it from another Infinity isn’t a number anymore.

// both `Infinity` and numbers are numbers
typeof Infinity      // ⇒ "number"
typeof 1000          // ⇒ "number"

// regular calculations with `Infinity` still return `Infinity`
Infinity + 1000      // ⇒ Infinity
Infinity - 2000      // ⇒ Infinity
Infinity * 3000      // ⇒ Infinity
Infinity / 4000      // ⇒ Infinity

// some of these return `NaN` when we repeat the calculation with `Infinity`
Infinity + Infinity  // ⇒ Infinity
Infinity - Infinity  // ⇒ NaN
Infinity * Infinity  // ⇒ Infinity
Infinity / Infinity  // ⇒ NaN

More fire tips

Replacing all matches in a string

The String prototype’s replace function only replaces the first occurrence of a substring by default. We can extend that with a global flag on the expression.

JavaScriptregular expressions
Read fire tip

Read all fire tips →