FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
JavaScriptArray restructuring

Split an array into bounded batches

JavaScript: Advance by a positive batch size and allow a shorter final chunk.

JavaScript✓ Tested
JavaScriptStrings

Split and trim text

Split simple comma-separated JavaScript text and trim each part.

JavaScript✓ Tested
JavaScriptRegular expressions & URL encoding

Split text on several separator characters

JavaScript: A regex character class can recognize alternative simple delimiters.

JavaScript✓ Tested
JavaScriptGetting Started

Store a value in a named variable

JavaScript: Give a value a descriptive name and print it.

JavaScript✓ Tested
JavaScriptArrays

Sum numeric values

Provide an initial accumulator for JavaScript reduce, including empty arrays.

JavaScript✓ Tested
JavaScriptPromises & iteration protocols

Transform a resolved promise result

JavaScript: A then handler returns the value passed to the next promise in the chain.

JavaScript✓ Tested
JavaScriptArrays

Transform array values

Use map to create a JavaScript array of squared values.

JavaScript✓ Tested
JavaScriptText handling & Unicode

Transform each regex match with a callback

JavaScript: A replacement callback computes the replacement separately for each match.

JavaScript✓ Tested
JavaScriptText handling & Unicode

Trim and discard blank lines

JavaScript: Split a text block, trim each line, and keep nonempty values.

JavaScript✓ Tested
JavaScriptStrings

Trim surrounding whitespace

Remove leading and trailing JavaScript string whitespace.

JavaScript✓ Tested
JavaScriptGetting Started

Update a running count

JavaScript: Change a variable after its initial value has been assigned.

JavaScript✓ Tested
JavaScriptPromises & iteration protocols

Use the first fulfilled promise result

JavaScript: Promise.any ignores individual rejections until one input fulfills.

JavaScript✓ Tested