
JavaScript code examples
Browse 275 JavaScript code examples across 29 topics, including arrays, objects, strings.
Jump to a topic
Arrays
Objects
Strings
Functions
Async
Numbers
Dates and time
Collections
Web primitives
Algorithms
Array methods
- Find the first matching index
- Find the last matching item
- Replace an array item immutably
- Splice a copy of an array
- Reverse an array without mutation
- Sort a copy by a field
- Check every item against a rule
- Check whether any item matches
- Flatten zero or more results per item
- Detect holes versus undefined values
Modern objects
- Group into a prototype-free object
- Read safely through a missing object
- Keep false and zero with nullish defaults
- Assign a value only when missing
- Make a property non-enumerable
- Read all own keys including symbols
- Store a derived getter on an object
- Rename nested destructured fields
- Create a dictionary without a prototype
- Swap variables with destructuring
Internationalization
Classes and protocols
- Define a private counter field
- Build an instance with a static factory
- Call the parent method from a subclass
- Make a class iterable
- Customize JSON serialization
- Preserve an error cause
- Collect multiple errors
- Identify a branded instance with instanceof
- Validate assignment with a setter
- Initialize static data in a block
Iterators and generators
- Yield values from several sources
- Stop a generator and run cleanup
- Send a value into a generator
- Take a finite prefix from an endless iterator
- Filter lazily with iterator helpers
- Skip the first iterator items
- Find a match in an iterator
- Iterate Unicode safely with for-of
- Combine nested iterators with flatMap
- Convert a synchronous iterable of promises to values
Node and testing
Streams and asynchronous work
- Transform a Web Stream
- Decode UTF-8 split across byte chunks
- Read a byte range from a Blob
- Drain a Node readable stream
- Pass stream chunks through a pipeline
- Combine abort signals
- Resolve a promise through an external callback
- Schedule a microtask after synchronous work
- Cancel a promise-based Node timer
- Flush asynchronous cleanup before returning
Data interchange
- Select JSON fields with a replacer list
- Rehydrate dates in a JSON reviver
- Read newline-delimited JSON
- Serialize a map as entries
- Read an internationalized hostname
- Canonicalize query parameters by key
- Replace duplicate fields in FormData
- Convert a byte array to hexadecimal
- Parse hexadecimal into raw bytes
- Read a signed integer with explicit endianness
Algorithms and collections
- Build a prefix sum
- Merge overlapping intervals
- Convert a spreadsheet column to its number
- Convert a number to another radix
- Find the first boundary in a sorted array
- Find items exclusive to either set
- Check that two sets have no common elements
- Track metadata with weak object keys
- Group by object identity
- Deduplicate records by ID while keeping the latest
Practical transformations
- Round a number toward zero
- Check whether an integer is safe to represent
- Sum distances with a Euclidean norm
- Separate a path segment from its query
- Validate a whole integer string before parsing
- Read an object with a Symbol key
- Escape a literal regular-expression fragment
- Trim indentation from a template literal
- Decode JSON with a fallback for malformed input
- Create accessible initials from names
Original archive
- Block Scope · code 01
- Es10 Es2019 · code 01
- Es5 2009 · code 01
- Es6 Es2015 · code 01
- Es7 Es2016 · code 01
- Es8 Es2017 · code 01
- Es9 Es2018 · code 01
- Counting Sort 1 · code 01
- Diagonal Difference · code 01
- Flipping Bits · code 01
- Lonely Integer · code 01
- Min Max Sum · code 01
- Plus Minus · code 01
- Sparse Arrays · code 01
- Time Conversion · code 01
- Fibonacci Sequence · code 01
- Fibonacci Sequence · code 02