The snippet library.
Small, practical examples. Search by task or narrow things down below.
Calculate a page count with ceiling division
JavaScript: Use Math.ceil for a nonnegative item count and a positive page size.
JavaScript✓ Tested
Calculate integers beyond Number’s precise range
JavaScript: Use BigInt operands together to avoid precision loss.
JavaScript✓ Tested
Check whether a value is a safe integer
JavaScript: Number.isSafeInteger also checks the exact-integer range supported by Number.
JavaScript✓ Tested
Clamp a number between inclusive bounds
JavaScript: Combine Math.min and Math.max when lower is no greater than upper.
JavaScript✓ Tested
Compare bounded floating-point results with a tolerance
JavaScript: Use an absolute error threshold for values known to be near one.
JavaScript✓ Tested
Convert between integer bases
JavaScript: Supply a radix when parsing a nondecimal integer string.
JavaScript✓ Tested
Detect NaN without converting the input
JavaScript: Number.isNaN tests the special numeric value instead of applying string-to-number conversion.
JavaScript✓ Tested
Display a number with a fixed decimal width
JavaScript: toFixed returns a formatted string rather than a numeric value.
JavaScript✓ Tested
Drop the fractional part toward zero
JavaScript: Math.trunc differs from floor for negative inputs.
JavaScript✓ Tested
Validate a finite numeric value without coercion
JavaScript: Number.isFinite rejects numeric strings as well as NaN and infinity.
JavaScript✓ Tested