Cheat sheets
Cheat sheets
The most important things on one page – useful JavaScript, array methods, React hooks and TypeScript in React, the most used first. Print them or keep them open next to your code.
Values and conversions
| I want… | Code |
|---|---|
| a variable that won’t change / will change | const name = 'Anna' let count = 0 |
| to find out the type of a value | typeof x // 'string', 'number'… |
| text to a number | Number('42') // NaN when it can't |
| is it a valid number? | Number.isNaN(n) // true = not a number |
| a number to text | String(42) |
| yes/no from anything | Boolean('') // false |
| compare exactly (always) | a === b a !== b |
In detail: lesson JavaScript 01.
Text
| I want… | Code |
|---|---|
| to put a value into text | `Hello, ${name}!` |
| to trim spaces | s.trim() |
| upper / lower case | s.toUpperCase() s.toLowerCase() |
| contains? starts? ends? | s.includes('x') s.startsWith('x') s.endsWith('x') |
| a piece of the text | s.slice(0, 5) s.slice(-3) |
| to replace every occurrence | s.replaceAll('-', '/') |
| to split into an array / join an array | s.split(', ') arr.join(', ') |
| to pad with a zero on the left | String(7).padStart(2, '0') // '07' |
| to sort alphabetically by language | a.localeCompare(b, 'en') |
Numbers
| I want… | Code |
|---|---|
| to round / down / up | Math.round(x) Math.floor(x) Math.ceil(x) |
| to two decimal places (a number) | Math.round(x * 100) / 100 |
| the largest / smallest | Math.max(...nums) Math.min(...nums) |
| the remainder (even/odd) | n % 2 === 0 |
| a random whole number 1–6 | Math.floor(Math.random() * 6) + 1 |
| a number for people (separators) | n.toLocaleString('en-US') |
| a price in dollars | new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(n) |
In detail: lesson JavaScript 01 and lesson JavaScript 08.
Objects
| I want… | Code |
|---|---|
| to read a property (also by a variable) | user.name user[key] |
| to unpack properties into variables | const { name, city = '?' } = user |
| a copy with a change | { ...user, city: 'Boston' } |
| without one property | const { password, ...rest } = user |
| keys / values / pairs | Object.keys(o) Object.values(o) Object.entries(o) |
| to go through all pairs | for (const [key, value] of Object.entries(o)) { … } |
| to translate a value with a dictionary | LABELS[status] ?? 'Unknown' |
In detail: lesson JavaScript 03.
Conditions in one expression
| I want… | Code |
|---|---|
| one of two | isDone ? 'Done' : 'To do' |
| a default only for null/undefined | count ?? 0 |
| to read only when the object exists | user?.address?.city |
| something or nothing (in JSX) | items.length > 0 && <List /> |
| to end a function early | if (!user) return null |
In detail: lesson JavaScript 06.
Functions
| I want… | Code |
|---|---|
| a short function with a result | const double = (n) => n * 2 |
| to return an object from an arrow | (name) => ({ name }) |
| a default parameter value | function greet(name = 'host') { … } |
| any number of arguments | function sum(...nums) { … } |
| to pass a function for later | setTimeout(() => greet('Anna'), 1000) |
In detail: lesson JavaScript 02.
Waiting and the server
| I want… | Code |
|---|---|
| to wait for a result | const user = await fetchUser(1) |
| to catch an error | try { … } catch (e) { … } finally { … } |
| several things at once | const [a, b] = await Promise.all([loadA(), loadB()]) |
| to load JSON from a server | const data = await (await fetch('/api/x')).json() |
| to call later / repeatedly | setTimeout(fn, ms) setInterval(fn, ms) |
In detail: lesson JavaScript 07.
Built-in helpers
| I want… | Code |
|---|---|
| an object to text and back | JSON.stringify(o) JSON.parse(text) |
| to save in the browser | localStorage.setItem('key', JSON.stringify(o)) |
| today's date for people | new Date().toLocaleDateString('en-GB') |
| the current time in ms | Date.now() |
| a deep copy | structuredClone(o) |
| a unique id | crypto.randomUUID() |
In detail: lesson JavaScript 08.