The snippet library.
Small, practical examples. Search by task or narrow things down below.
Consume an asynchronous sequence
JavaScript: for await waits for each value of an async iterator in sequence.
JavaScript✓ Tested
Convert between integer bases
JavaScript: Supply a radix when parsing a nondecimal integer string.
JavaScript✓ Tested
Convert epoch milliseconds to whole seconds
JavaScript: JavaScript Date stores milliseconds while many APIs use Unix seconds.
JavaScript✓ Tested
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
Copy a range without changing the source
JavaScript: Array.slice returns a shallow copy of the selected half-open range.
JavaScript✓ Tested
Copy nested structured data independently
JavaScript: structuredClone creates an independent copy of supported structured values.
JavaScript✓ Tested
Count days between UTC-midnight dates
JavaScript: A day is exactly 86400000 milliseconds for these explicitly UTC-midnight inputs.
JavaScript✓ Tested
Count the items in a collection
JavaScript: Read the collection’s length instead of hard-coding the loop limit.
JavaScript✓ Tested
Count Unicode code points in a string
JavaScript: String iteration combines surrogate pairs, unlike the UTF-16 length property.
JavaScript✓ Tested
Create independent rows in a matrix
JavaScript: Use a mapping function to create each row instead of sharing one array with fill.
JavaScript✓ Tested
Decode a percent-encoded URL component
JavaScript: decodeURIComponent reverses percent encoding and throws for malformed sequences.
JavaScript✓ Tested
Delegate iteration to another iterable
JavaScript: yield-star forwards items from another iterable while retaining generator composition.
JavaScript✓ Tested