JavaScript Quick Reference
Modern JavaScript: variables, strings, arrays, objects, functions, async/await, classes, modules and the DOM.
10 sections · 85 snippets · updated Oct 14, 2026
Click any snippet to copy it.
Basics
Variables, output and types.
TaskCode
- Constant / variable
- Log to the console
- Comments
- Check a type
- Strict equality
- Default for null/undefined
- Optional chaining
- Destructuring
- Spread
Numbers & conversion
Math and converting values.
TaskCode
- String to number
- Number to string
- Round
- Random integer 1–6
- Min / max
- Format a number
- Is it a number?
Strings
Working with text.
TaskCode
- Template literal
- Length
- Upper / lower case
- Trim
- Contains / starts / ends
- Replace all
- Split / join
- Slice
- Pad
- Repeat
Arrays
Creating, changing and transforming arrays.
TaskCode
- Create
- First / last
- Add / remove at the end
- Add / remove at the start
- Remove by index
- Transform each item
- Keep matching items
- Find an item
- Check items
- Sum
- Sort numbers
- Sort strings
- Remove duplicates
- Reverse
- Flatten
- Group by
- Loop
Objects
Key–value data.
TaskCode
- Create
- Read / write a property
- Delete a property
- Has a property
- Keys / values / entries
- Loop over entries
- Merge / copy
- From entries
- Map (any key type)
- JSON
Control flow
Conditions and loops.
TaskCode
- If / else
- Ternary
- Switch
- For loop
- While loop
- Try / catch
- Throw an error
Functions
Declaring and calling functions.
TaskCode
- Function declaration
- Arrow function
- Default parameter
- Rest parameters
- Callback
- Timers
Async
Promises, async/await and fetch.
TaskCode
- Async function
- Handle errors
- Run in parallel
- POST JSON
- Wait (sleep)
- Promise chain
Classes & modules
Classes and ES modules.
TaskCode
- Class
- Inheritance
- Private field / static
- Export
- Import
- Dynamic import
DOM
Working with the page in the browser.
TaskCode
- Select elements
- Change text / HTML
- Classes
- Attributes
- Create and add an element
- Listen for events
- Local storage