The snippet library.
Small, practical examples. Search by task or narrow things down below.
Parse JSON text
Read a JavaScript object from JSON text.
JavaScript✓ Tested
Percent-encode one URL component
JavaScript: encodeURIComponent escapes a parameter value rather than a complete URL.
JavaScript✓ Tested
Prevent runtime changes to top-level properties
JavaScript: Object.freeze prevents ordinary top-level mutations; Reflect.set reports the rejected update without throwing.
JavaScript✓ Tested
Print a number with two decimal places
JavaScript: Format a small decimal value consistently for display.
JavaScript✓ Tested
Produce a lazy sequence with a generator
JavaScript: A generator yields one value per iteration without building an intermediate array.
JavaScript✓ Tested
Read a nested value safely
Combine optional chaining with nullish coalescing in JavaScript.
JavaScript✓ Tested
Read a URL query parameter
Parse a URL with the standard JavaScript URL API.
JavaScript✓ Tested
Read a UTF-8 file
Read a small file as text using Node.js.
JavaScript✓ Tested
Read an ISO-style weekday number
JavaScript: Convert Sunday from zero to seven when using Monday-through-Sunday numbering.
JavaScript✓ Tested
Read an object’s own enumerable values
JavaScript: Object.values provides values without their corresponding keys.
JavaScript✓ Tested
Read date components in UTC
JavaScript: UTC accessors avoid differences caused by the machine’s local timezone.
JavaScript✓ Tested
Read named regex capture groups
JavaScript: Named groups make a fixed input format easier to inspect than numeric group positions.
JavaScript✓ Tested