Language cheatsheet
JavaScript 2025 Cheatsheet
Modern JavaScript for arrays, strings, objects, async code, the DOM and modules, in copy-ready snippets.
Arrays
- Transform every item — Create a new array by applying a function to each item.
- Keep matching items — Create a new array with the items that pass a test.
- Combine items into one value — Add up a property across an array. The second argument is the starting value.
- Find the first match — Return the first item that passes a test, or undefined if none does.
- Test items against a condition — Check whether any item, or every item, passes a test.
- Check if an array contains a value — Return true if the value is in the array.
- Map and flatten in one step — Collect the tags of every post into a single array.
- Sort numbers without changing the original — Sort ascending. sort() compares as text by default and changes the array, so toSorted with a comparator is safer.
- Copy an array — Make a shallow copy with spread.
- Get the last item — Use a negative index with at().
- Create a range of numbers — Build an array of the numbers 0 to 4.
- Remove duplicates — Convert to a Set and back to an array.
- Group items by a property — Return an object whose keys are the group names. Available in current browsers and Node.js 21 and later.
Strings
- Insert values into a string — Use backticks and ${} to build strings, including multi-line ones.
- Split and join — Turn a string into an array, and back again.
- Replace every occurrence — Replace all matches, not just the first.
- Pad a string — Pad on the left, for example to make 7 into 07.
- Check the start, end or contents — Test a string without a regular expression.
- Capitalize a word — Uppercase the first letter and keep the rest.
- Sort text correctly — Sort strings using the language's rules, including accents and capitals.
- Format numbers and currency — Format with the user's locale conventions.
- Format a date — Format a date for a locale.
Objects and data
- Destructure an object — Pull properties into variables, with a default for a missing one.
- Swap two variables — Use array destructuring to swap without a temporary variable.
- Merge objects — Combine objects. Later properties win.
- Loop over an object — Iterate over keys and values.
- Build an object from pairs — Turn an array of [key, value] pairs into an object.
- Read nested properties safely — Return undefined instead of throwing when something in the chain is null or undefined.
- Use a default only for null or undefined — Unlike ||, it keeps 0 and empty strings.
- Deep copy an object — Copy nested data, including dates and maps.
- Convert to and from JSON — Pretty-print an object as JSON, and parse JSON text back.
Promises and fetch
- Fetch JSON — Request JSON and check for HTTP errors. fetch only rejects on network failures.
- POST JSON — Send a JSON body with the right header.
- Time out a request — Abort a request that takes longer than five seconds.
- Handle errors in async code — Catch rejected promises and thrown errors with try and catch.
- Run promises in parallel — Wait for all promises. It rejects as soon as one fails.
- Wait for all promises, whatever happens — Get the outcome of every promise, including failures.
- Pause for a while — A promise that resolves after a delay.
- Debounce a function — Run a function only after calls stop for a moment, for example on search input.
DOM and browser
- Find elements — Get the first element that matches a CSS selector, or all matches.
- Listen for events — Run a function when the user clicks.
- Handle events for many elements — Listen once on a parent and check which child was clicked.
- Add, remove or toggle a class — Change CSS classes on an element.
- Create and add an element — Build an element and append it to the page.
- Set text safely — textContent treats the value as plain text. innerHTML parses HTML.
- Read data attributes — Read data-user-id from an element.
- Read a submitted form — Stop the page reload and read the form fields as an object.
- Store data in the browser — Save and load JSON in localStorage. It only stores strings.
- Run code when the page is ready — Wait for the HTML to be parsed. Scripts with defer or type=module don't need this.
Modules
- Export and import by name — Share functions between files with ES modules.
- Default export — Export one main value per file.
- Load a module on demand — Import code only when it is needed.
- Load a module in HTML — Modules are deferred and run in strict mode automatically.
- Import a Node.js built-in — Use the node: prefix for built-in modules.
Related cheatsheets
- C++ 23 cheatsheet — Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
- C# 14 cheatsheet — Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- Go 1 cheatsheet — The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.