JavaScript Numbers Cheatsheet
š¢ JavaScript Numbers Cheatsheet
JavaScript numbers are represented as IEEE-754 double-precision floats.
This cheatsheet provides a complete reference of number constants, methods, special values, parsing & formatting helpers, and so on.
ā Number constants ā Number methods ā Special values ā Parsing numbers ā Formatting numbers ā Math helpers
#javascript #numbers #bigint #NaN #math #parseint #parsefloat #floatingpoint #precision #webdev
Number constants
| Constant | Description |
|---|---|
Number.EPSILON | smallest difference between two representable numbers. (2.220446049250313e-16) |
Number.MAX_VALUE | Largest finite number. (1.7976931348623157e+308) |
Number.MIN_VALUE | Smallest positive number. (5e-324) |
Number.MAX_SAFE_INTEGER | Largest safe integer. (9007199254740991) |
Number.MIN_SAFE_INTEGER | Smallest safe integer. (-9007199254740991) |
Number.POSITIVE_INFINITY | Positive infinity. |
Number.NEGATIVE_INFINITY | Negative infinity. |
Number.NaN | Not-a-Number. |
Number methods
| Method | Description |
|---|---|
Number.isNaN(value) | Returns true if value is NaN. |
Number.isFinite(value) | Returns true if value is a finite number. |
Number.isInteger(value) | Returns true if value is an integer. |
Number.isSafeInteger(value) | Returns true if value is a safe integer. |
Number.parseFloat(string) | Parses a string and returns a floating-point number. |
Number.parseInt(string, radix) | Parses a string and returns an integer. |
Number.toFixed(digits) | Returns a string with a fixed number of decimals. |
Number.toPrecision(precision) | Returns a string with a specified precision. |
Number.toString(radix) | Returns a string representation in the given base. |
Number.toExponential(fraction) | Returns a string in exponential notation. |
Number.valueOf() | Returns the primitive value of a Number object. |
Parsing numbers
Number.parseInt()
Parses a string and returns an integer. Always specify the base to avoid unexpected results.
Number.parseInt("42", 10); // 42
Number.parseInt("ff", 16); // 255
Number.parseInt("10px"); // 10Number.parseFloat()
Parses a string and returns a floating-point number.
Number.parseFloat("3.14"); // 3.14
Number.parseFloat("10.5px"); // 10.5Number()
Converts a value to a number. Whitespace is ignored.
Number(" 100 "); // 100
Number("3.14"); // 3.14
Number(""); // 0
Number("hello"); // NaN+ (unary plus)
The unary plus operator converts a string to a number. Prefer Number() for clarity.
+"99"; // 99
+"3.14"; // 3.14
+true; // 1Formatting numbers
toFixed()
Returns a string with a fixed number of decimal places.
(1.2345).toFixed(2); // "1.23"
(10).toFixed(4); // "10.0000"toPrecision()
Returns a string with a specified total number of significant digits.
(1.2345).toPrecision(3); // "1.23"
(1234).toPrecision(2); // "1.2e+3"toString()
Returns a string representation in the given base.
(255).toString(16); // "ff"
(8).toString(2); // "1000"toLocaleString()
Returns a language-sensitive formatted string.
(1234567).toLocaleString("en-US"); // "1,234,567"
(1234.5).toLocaleString("de-DE"); // "1.234,5"toExponential()
Returns a string in exponential notation.
(12345).toExponential(2); // "1.23e+4"Math helpers
Math.round()
Rounds to the nearest integer.
Math.round(4.4); // 4
Math.round(4.5); // 5Math.floor()
Rounds down to the nearest integer.
Math.floor(4.9); // 4
Math.floor(-4.1); // -5Math.ceil()
Rounds up to the nearest integer.
Math.ceil(4.1); // 5
Math.ceil(-4.9); // -4Math.trunc()
Removes the decimal part.
Math.trunc(4.9); // 4
Math.trunc(-4.9); // -4Math.abs()
Returns the absolute value.
Math.abs(-5); // 5Math.max() / Math.min()
Return the largest or smallest of the given values.
Math.max(1, 5, 3); // 5
Math.min(1, 5, 3); // 1Math.random()
Returns a random number from 0 (inclusive) to 1 (exclusive).
Math.random(); // e.g., 0.374829Math.pow() / Math.sqrt()
Exponentiation and square root.
Math.pow(2, 3); // 2^3 -> 8
Math.sqrt(16); // 4Special values
0.1 + 0.2 === 0.3
Returns false due to floating-point precision. Use rounding or Number.EPSILON for comparisons.
Number.isNaN(NaN)
Returns true. Always use Number.isNaN instead of === because NaN === NaN is false.
Number.isFinite(Infinity)
Returns false. Infinity and -Infinity are not finite numbers.
Number.isInteger(42)
Returns true for whole numbers without a fractional part.
Number.isSafeInteger(9007199254740992)
Returns false. Safe integers are those that can be exactly represented as a double.
Full-Stack AI Developer Roadmap
From HTML & CSS to working with AI models, all in one structured roadmap.