JavaScript Cheat Sheet
Quick reference from FreeCodingCourses.com
Variables and types
const name = 'Ada'- block-scoped, cannot be reassigned. Default choice.
let count = 0- block-scoped, reassignable. Use when the value changes.
// avoid var- var is function-scoped and legacy. Use let/const.
typeof x- 'string', 'number', 'boolean', 'object', 'undefined'.
null undefined- null is a deliberate empty; undefined is not set yet.
Strings and template literals
`Hi ${name}, age ${age}`- template literal with backticks. Inline expressions.
s.length- character count (a property, no parentheses).
s.toUpperCase() s.trim()- case change and trim whitespace.
s.includes('foo')- true if the substring is present.
s.split(',') arr.join('-')- string to array and back.
s.slice(0, 4)- substring from index 0 up to 4.
Arrays
const xs = [1, 2, 3]- ordered list.
xs.push(4) xs.pop()- add or remove at the end.
xs.unshift(0) xs.shift()- add or remove at the start.
xs.length- item count.
xs.slice(1, 3)- copy a range without changing xs.
const [a, b] = xs- destructure into variables.
Array methods (the daily ones)
xs.map(n => n * 2)- transform every item, returns a new array.
xs.filter(n => n > 0)- keep items that pass the test.
xs.find(n => n === 3)- first matching item, or undefined.
xs.reduce((sum, n) => sum + n, 0)- fold to a single value.
xs.forEach(n => console.log(n))- run a function per item (no return).
xs.some(...) xs.every(...)- true if any / all pass.
Objects
const user = { name: 'Ada', age: 36 }- key/value pairs.
user.name user['name']- dot or bracket access.
const { name, age } = user- destructure properties.
const copy = { ...user, age: 37 }- spread to copy and override.
Object.keys(user) Object.entries(user)- get keys, or key/value pairs.
'name' in user- check a property exists.
Functions and control flow
const add = (a, b) => a + b- arrow function, implicit return.
function add(a, b) { return a + b }- classic declaration.
const g = (name = 'friend') => ...- default parameter.
if (x > 0) { ... } else { ... }- braces define blocks.
const label = x > 0 ? 'pos' : 'neg'- ternary: condition ? a : b.
for (const item of items) { ... }- loop over array values.
Async / await
const res = await fetch(url)- await pauses until the promise resolves.
const data = await res.json()- parse the JSON body.
async function load() { ... }- await only works inside an async function.
try { await ... } catch (e) { ... }- catch rejected promises.
Promise.all([a, b, c])- run promises in parallel, wait for all.
Unlock the full JavaScript cheat sheet
You're seeing 2 of 7 sections. Drop your email to open the rest plus the gotchas, and save the whole thing as a printable PDF.
No spam, unsubscribe anytime. See our privacy policy.