FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
JavaScriptPromises & iteration protocols

Consume an asynchronous sequence

JavaScript: for await waits for each value of an async iterator in sequence.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Convert between integer bases

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

JavaScript✓ Tested
JavaScriptUTC date operations

Convert epoch milliseconds to whole seconds

JavaScript: JavaScript Date stores milliseconds while many APIs use Unix seconds.

JavaScript✓ Tested
JavaScriptGetting Started

Convert known numeric text to an integer

JavaScript: Convert a trusted literal to an integer before adding. Validate external input before converting it.

JavaScript✓ Tested
JavaScriptArray traversal & selection

Copy a range without changing the source

JavaScript: Array.slice returns a shallow copy of the selected half-open range.

JavaScript✓ Tested
JavaScriptObject behavior & collections

Copy nested structured data independently

JavaScript: structuredClone creates an independent copy of supported structured values.

JavaScript✓ Tested
JavaScriptUTC date operations

Count days between UTC-midnight dates

JavaScript: A day is exactly 86400000 milliseconds for these explicitly UTC-midnight inputs.

JavaScript✓ Tested
JavaScriptGetting Started

Count the items in a collection

JavaScript: Read the collection’s length instead of hard-coding the loop limit.

JavaScript✓ Tested
JavaScriptText handling & Unicode

Count Unicode code points in a string

JavaScript: String iteration combines surrogate pairs, unlike the UTF-16 length property.

JavaScript✓ Tested
JavaScriptArray restructuring

Create independent rows in a matrix

JavaScript: Use a mapping function to create each row instead of sharing one array with fill.

JavaScript✓ Tested
JavaScriptRegular expressions & URL encoding

Decode a percent-encoded URL component

JavaScript: decodeURIComponent reverses percent encoding and throws for malformed sequences.

JavaScript✓ Tested
JavaScriptPromises & iteration protocols

Delegate iteration to another iterable

JavaScript: yield-star forwards items from another iterable while retaining generator composition.

JavaScript✓ Tested