JavaScript

JavaScript is Weird

Ethan Glover

JavaScript can be strange. Like any programming language. Here are some things to help with that.

Checking if numbers can be coerced

isNan(n) returns true if n is NaN or is a value that cannot be coerced into a number. (Note that Number(null) === 0).

1isNaN(0/0) // true

Number.isNaN(n) returns true only if n is NaN.

1Number.isNaN(0/0) // true

The solution:

1export function isNumber(value: unknown): boolean {
1isNumber(0/0) // false

Float Imprecision

1function areFloatsEqual(x, y, epsilon = 1e-10) {
10.1 + 0.2 === 0.3 // false

Try It Out:

10.1 + 0.2 === 0.3 // false
1areFloatsEqual(0.3, 0.1 + 0.2, 1e-10) // true