The snippet library.
Small, practical examples. Search by task or narrow things down below.
Check whether every item matches
JavaScript: every stops at the first failed predicate and returns true for an empty array.
JavaScript✓ Tested
Check whether text ends with a suffix
JavaScript: Use endsWith for a literal case-sensitive ending.
JavaScript✓ Tested
Check whether text starts with a prefix
JavaScript: Use startsWith for a literal case-sensitive prefix comparison.
JavaScript✓ Tested
Choose between two branches
JavaScript: Use if and else to handle an available or empty stock count.
JavaScript✓ Tested
Clamp a number between inclusive bounds
JavaScript: Combine Math.min and Math.max when lower is no greater than upper.
JavaScript✓ Tested
Clean up a generator after an early break
JavaScript: Breaking a for-of loop closes a generator and runs its finally block.
JavaScript✓ Tested
Combine text with a name
JavaScript: Construct a greeting from a fixed prefix and a stored name.
JavaScript✓ Tested
Compare a value with a limit
JavaScript: Turn a comparison into a readable yes or no result.
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
Compare canonically equivalent Unicode text
JavaScript: Normalize to NFC before comparing equivalent composed and decomposed text.
JavaScript✓ Tested
Concatenate arrays without mutating either input
JavaScript: concat creates a shallow combined sequence.
JavaScript✓ Tested
Construct a date from Unix seconds
JavaScript: Multiply seconds by 1000 before creating a Date.
JavaScript✓ Tested