The snippet library.
Small, practical examples. Search by task or narrow things down below.
Calculate differences between adjacent readings
JavaScript: Slice off the first value and compare each remaining value with its predecessor.
JavaScript✓ Tested
Concatenate arrays without mutating either input
JavaScript: concat creates a shallow combined sequence.
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
Flatten a single nesting level
JavaScript: flat defaults to a depth of one and creates a new array.
JavaScript✓ Tested
Generate a bounded integer range
JavaScript: Array.from supplies each output position to its mapping function.
JavaScript✓ Tested
Keep both matching and rejected values
JavaScript: Use two explicit outputs when a caller needs both sides of a classification.
JavaScript✓ Tested
Map items to multiple output values
JavaScript: flatMap combines mapping and a one-level flattening operation.
JavaScript✓ Tested
Replace an array segment in place
JavaScript: splice returns removed items and inserts replacements at the chosen position.
JavaScript✓ Tested
Reverse a copy while preserving the original
JavaScript: Copy before reverse because reverse mutates the array it receives.
JavaScript✓ Tested
Split an array into bounded batches
JavaScript: Advance by a positive batch size and allow a shorter final chunk.
JavaScript✓ Tested