Javascript: Working With Numbers

📄 Wiki page | 🕑 Last updated: Feb 9, 2022

There is no separation between floats and integers, there is only 'number' type (and 'bigint' in ES11+).

42.0 === 42

Arithmetic operations mostly work as expected:

40 + 2 // 42
50 - 8 // 42
2 * 21 // 42
84 / 2 // 42
85 / 2 // 42.5

(4 - 2) * 21 // 42
(85 % 2) // 1 (modulo division)

Bitwise operations also work as expected:

4 & 2 // 0 - AND
4 | 2 // 6 - OR
4 ^ 2 // 6 - XOR
~42 // -43 - NOT
4 << 2 // 16 - zero fill left shift
4 >>> 2 // 1 - zero fill right shift
4 >> 2 // 1 - signed right shift

Beside the normal numbers, there are three special values: NaN, Infinity and -Infinity

0/0 // NaN - Not a Number
1/0 // Infinity
-1/0 // -Infinity

All numeric values except 0 and NaN are considered truthy. Use === when you need strict equality.

!0 // true
!NaN // true
!42 // false

42 == "42" // true
42 === "42" // false

Ask me anything / Suggestions

If you have any suggestions or questions (related to this or any other topic), feel free to contact me. ℹī¸


If you find this site useful in any way, please consider supporting it.