FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

10 snippets foundClear filters
JavaScriptNumbers & numeric validation

Calculate a page count with ceiling division

JavaScript: Use Math.ceil for a nonnegative item count and a positive page size.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Calculate integers beyond Number’s precise range

JavaScript: Use BigInt operands together to avoid precision loss.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Check whether a value is a safe integer

JavaScript: Number.isSafeInteger also checks the exact-integer range supported by Number.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Clamp a number between inclusive bounds

JavaScript: Combine Math.min and Math.max when lower is no greater than upper.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Compare bounded floating-point results with a tolerance

JavaScript: Use an absolute error threshold for values known to be near one.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Convert between integer bases

JavaScript: Supply a radix when parsing a nondecimal integer string.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Detect NaN without converting the input

JavaScript: Number.isNaN tests the special numeric value instead of applying string-to-number conversion.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Display a number with a fixed decimal width

JavaScript: toFixed returns a formatted string rather than a numeric value.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Drop the fractional part toward zero

JavaScript: Math.trunc differs from floor for negative inputs.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Validate a finite numeric value without coercion

JavaScript: Number.isFinite rejects numeric strings as well as NaN and infinity.

JavaScript✓ Tested