This javascript cheat sheet gathers the syntax you look up most often. The array methods follow the Array reference on MDN Web Docs. [1] It covers variables and functions, promises and async functions, and a few DOM calls for working with a page.
How to use the javascript cheat sheet
- Type in the search box to filter by any word in a name, a command or its note.
- Choose a topic to see one group instead of the whole list.
- Read the command in the right-hand column and its meaning on the left.
- Press Print for a clean copy of whatever is shown.
What is in each topic
The javascript cheat sheet is split into 5 topics. The table shows how many entries each topic holds and which ones they are, so you can jump straight to the right group with the topic box.
| Topic | Entries | What it covers |
|---|---|---|
| Array methods | 7 | add to the end; remove the last; remove the first; add to the front; copy a section; add or remove at an index; item by position |
| Transforming arrays | 8 | map; filter; reduce; find one; contains; sorted copy; join; flatten |
| Variables and functions | 5 | constant; variable; function; arrow function; template string |
| Promises and async | 4 | promise then; async function; await a promise; catch an error |
| The page | 4 | find one element; find many; listen for events; change text |
Array methods that change the array
push adds one or more elements to the end and returns the new length, pop removes the last element and returns it, shift removes the first and unshift adds to the front. splice adds or removes elements at an index, and sort sorts in place. [1] The MDN page notes that push, pop, shift, unshift, splice and sort mutate the original array, while the other methods return new arrays or values. [1]
Mutation is a common source of bugs when the same array is used in several places. If you need the original, copy it first or choose a method that returns a new array.
Array methods that return something new
slice extracts a section into a new array, map returns a new array of the results of calling a function on every element, and filter keeps the elements for which the function returns true. [1] reduce runs a callback over the elements to produce a single value, find returns the first element that passes a test, and includes returns true or false for whether a value is present. [1]
at returns the item at an index, with negative indexes counting from the end, flat concatenates sub-arrays to a given depth, join builds a string and toSorted returns a sorted copy without changing the original. [1]
Variables, functions and async code
Use const for values that are not reassigned and let for ones that are. An arrow function such as (a, b) => a + b is a short form of a function, and a template string with backticks embeds expressions in text. These syntax lines are listed from our own working.
fetch returns a promise, and .then runs when it resolves. An async function always returns a promise, and await pauses it until another promise settles. Wrap awaited calls in try and catch to handle errors. In the page, document.querySelector takes a CSS selector and returns the first match, and addEventListener runs a function when an event happens.
Who this page helps
Developers use a cheat sheet to look up a command they have not typed for a while, to learn the names of the options and to compare similar commands. Beginners can read down a topic to see what is possible, and experienced users can search for the one flag they forgot.
Tips for getting the most from the javascript cheat sheet
- Check whether an array method changes the original before you use it on shared data.
- Use const by default and switch to let only when a value must change.
- Handle failed requests with try and catch around await, so errors do not disappear silently.
Common mistakes
Most problems come from running a command in the wrong place or with the wrong target. Read a command before you press Enter, replace every placeholder with your own value, and try destructive commands on a test copy first. When a command behaves differently from the note, check the version you have installed and the official documentation.
Where the entries come from
Each command was checked against the documentation listed below this article. Where a command is standard but is not shown on the page we read, the article says it is our own working. Options and defaults change between releases, so run the help command for the version you have installed before you rely on a flag.
What this page does not do
It lists the commands most people need and does not try to cover every option. For the complete syntax, error messages and advanced features, open the official documentation linked in the sources, and keep it beside the cheat sheet while you learn.
Printing and checking
Filter by topic before you print if you only need one section, and the printed page will be shorter. The notes are short on purpose, so keep your textbook or the official documentation open for the full detail and for anything that changes between versions.
What next
Use this often? Add JavaScript C to your Home screen.
Frequently asked questions
What does this javascript cheat sheet cover?
Array methods, variables, functions, template strings, promises, async and await, error handling and basic DOM selection and events.
Which array methods change the original array?
push, pop, shift, unshift, splice and sort change it. The others, such as map, filter and slice, return a new array or value.
What is the difference between map and filter?
map returns a new array with a result for every element. filter returns only the elements for which your function returns true.
What does await do?
It pauses an async function until a promise settles, then gives you the result, so asynchronous code reads like ordinary code.
Sources
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.