FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
JavaScriptArray traversal & selection

Check whether every item matches

JavaScript: every stops at the first failed predicate and returns true for an empty array.

JavaScript✓ Tested
JavaScriptText handling & Unicode

Check whether text ends with a suffix

JavaScript: Use endsWith for a literal case-sensitive ending.

JavaScript✓ Tested
JavaScriptText handling & Unicode

Check whether text starts with a prefix

JavaScript: Use startsWith for a literal case-sensitive prefix comparison.

JavaScript✓ Tested
JavaScriptGetting Started

Choose between two branches

JavaScript: Use if and else to handle an available or empty stock count.

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
JavaScriptPromises & iteration protocols

Clean up a generator after an early break

JavaScript: Breaking a for-of loop closes a generator and runs its finally block.

JavaScript✓ Tested
JavaScriptGetting Started

Combine text with a name

JavaScript: Construct a greeting from a fixed prefix and a stored name.

JavaScript✓ Tested
JavaScriptGetting Started

Compare a value with a limit

JavaScript: Turn a comparison into a readable yes or no result.

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
JavaScriptText handling & Unicode

Compare canonically equivalent Unicode text

JavaScript: Normalize to NFC before comparing equivalent composed and decomposed text.

JavaScript✓ Tested
JavaScriptArray restructuring

Concatenate arrays without mutating either input

JavaScript: concat creates a shallow combined sequence.

JavaScript✓ Tested
JavaScriptUTC date operations

Construct a date from Unix seconds

JavaScript: Multiply seconds by 1000 before creating a Date.

JavaScript✓ Tested