Programming · Cheatsheet
JavaScript ES6+ Cheatsheet
Modern JavaScript syntax: destructuring, arrow functions, promises, modules, and built-in methods
59 commands 9 sections
No entry matches that filter.
-
const x = 1Block-scoped, read-only constant -
let y = 2Block-scoped variable -
const [a, b] = [1, 2]Array destructuring -
const { name, age } = objObject destructuring -
const { name: n } = objDestructuring with rename -
const { x = 10 } = objDestructuring with default
-
const fn = (a, b) => a + bArrow function (implicit return) -
const fn = (a) => { return a * 2 }Arrow function with body -
const fn = a => a * 2Single param (no parens needed) -
arr.map(x => x * 2)Arrow function as callback -
arr.filter(x => x > 5)Filter with arrow function
-
`Hello ${name}`Template literal with interpolation -
`multi\nline`Multi-line string -
str.includes("sub")Check if string contains substring -
str.startsWith("pre")Check if starts with -
str.endsWith("suf")Check if ends with -
str.repeat(3)Repeat string n times -
str.padStart(5, "0")Pad string from start -
str.trimStart() / str.trimEnd()Trim whitespace from start/end
-
[...arr1, ...arr2]Spread arrays (merge) -
{ ...obj1, ...obj2 }Spread objects (merge) -
const [first, ...rest] = arrRest in destructuring -
function fn(...args) {}Rest parameters -
fn(...args)Spread as function arguments
-
arr.map(fn)Transform each element -
arr.filter(fn)Keep elements matching condition -
arr.reduce(fn, init)Reduce to single value -
arr.find(fn)Find first matching element -
arr.findIndex(fn)Find index of first match -
arr.some(fn)Test if any element matches -
arr.every(fn)Test if all elements match -
arr.includes(val)Check if value exists -
arr.flat(depth)Flatten nested arrays -
arr.flatMap(fn)Map then flatten one level -
Array.from(iterable)Create array from iterable -
structuredClone(obj)Deep clone object/array
-
{ name, age }Shorthand property names -
{ [key]: value }Computed property names -
{ method() {} }Shorthand method -
Object.keys(obj)Get array of keys -
Object.values(obj)Get array of values -
Object.entries(obj)Get array of [key, value] pairs -
Object.assign({}, obj)Shallow copy / merge objects -
obj?.nested?.propOptional chaining -
val ?? defaultValNullish coalescing
-
new Promise((resolve, reject) => {})Create a promise -
promise.then(fn).catch(fn)Handle promise result/error -
async function fn() {}Async function declaration -
const result = await promiseAwait promise resolution -
Promise.all([p1, p2])Wait for all promises -
Promise.allSettled([p1, p2])Wait for all (resolved or rejected) -
Promise.race([p1, p2])First promise to settle wins
-
export const x = 1Named export -
export default fnDefault export -
import { x } from "./mod"Named import -
import fn from "./mod"Default import -
import * as mod from "./mod"Import all as namespace -
const mod = await import("./mod")Dynamic import
-
https://tc39.es/ecma262/multipage/TC39 ECMAScript specification: language syntax and built-in behavior.