FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
JavaScriptUTC date operations

Detect an invalid Date object

JavaScript: An invalid Date has a NaN time value.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Detect NaN without converting the input

JavaScript: Number.isNaN tests the special numeric value instead of applying string-to-number conversion.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Display a number with a fixed decimal width

JavaScript: toFixed returns a formatted string rather than a numeric value.

JavaScript✓ Tested
JavaScriptObject behavior & collections

Distinguish an own property from an inherited one

JavaScript: Object.hasOwn avoids depending on an object’s possibly shadowed hasOwnProperty method.

JavaScript✓ Tested
JavaScriptGetting Started

Divide using a fractional number

JavaScript: Use a floating-point operand to preserve the fractional part of a division.

JavaScript✓ Tested
JavaScriptNumbers & numeric validation

Drop the fractional part toward zero

JavaScript: Math.trunc differs from floor for negative inputs.

JavaScript✓ Tested
JavaScriptGetting Started

Explain a calculation with a comment

JavaScript: Keep the code executable while documenting why a fixed input is used.

JavaScript✓ Tested
JavaScriptRegular expressions & URL encoding

Extract all regex matches with their groups

JavaScript: matchAll requires a global regular expression and returns an iterator of matches.

JavaScript✓ Tested
JavaScriptText handling & Unicode

Extract text by start and end positions

JavaScript: String slice uses an inclusive start and exclusive end measured in UTF-16 code units.

JavaScript✓ Tested
JavaScriptText handling & Unicode

Extract the text after the last delimiter

JavaScript: Use lastIndexOf when earlier occurrences should remain part of the prefix.

JavaScript✓ Tested
JavaScriptArrays

Filter even numbers

Create a JavaScript array containing only even values.

JavaScript✓ Tested
JavaScriptObject behavior & collections

Find members absent from another set

JavaScript: Negate a membership test to calculate the set difference.

JavaScript✓ Tested