Everyday values
Introduction
Quick reference for everyday JavaScript: values, collections, dates, async work, and modules. Related cheatsheets are linked throughout.
- JavaScript reference (developer.mozilla.org)
- JavaScript Guide (developer.mozilla.org)
Strings
const name = 'Ada'
`Hello, ${name}!` // → "Hello, Ada!"
' hi '.trim() // → "hi"
'hello'.includes('ell') // → true
'a,b,c'.split(',') // → ["a", "b", "c"]
'hello'.slice(1, 3) // → "el"
Numbers
parseInt('42px', 10) // → 42
(1234.5).toFixed(2) // → "1234.50"
(1234).toLocaleString('en-US')
// → "1,234"
Math.max(1, 2, 3) // → 3
Number.isInteger(2.5) // → false
Arrays
const list = [1, 2, 3]
list.map(n => n * 2) // → [2, 4, 6]
list.filter(n => n > 1) // → [2, 3]
list.find(n => n > 1) // → 2
list.includes(2) // → true
[...list, 4] // → [1, 2, 3, 4]
Objects
const user = { name: 'Ada', age: 36 }
user.name // → "Ada"
user['age'] // → 36
Object.keys(user) // → ["name", "age"]
Object.entries(user)
// → [["name", "Ada"], ["age", 36]]
const { name, ...rest } = user
// rest → { age: 36 }
({ ...user, age: 37 })
// → { name: "Ada", age: 37 }
Collections
Map
const ages = new Map([['Ada', 36]])
ages.set('Bob', 40)
ages.get('Ada') // → 36
ages.has('Bob') // → true
[...ages.keys()] // → ['Ada', 'Bob']
Set
const seen = new Set([1, 2, 2])
// → Set {1, 2}
seen.add(3) // → Set {1, 2, 3}
seen.has(2) // → true
seen.size // → 3
[...seen] // → [1, 2, 3]
Dates and time
Date
const now = new Date()
now.toISOString()
// → ISO timestamp, UTC
now.getFullYear() // → 2026
Date.now() // → ms since epoch
new Date('2026-11-05')
// → UTC midnight
Temporal
const d = Temporal.PlainDate.from('2026-11-05')
d.add({ months: 1 }) // → 2026-12-05
Temporal.Now.instant()
// → current instant
Temporal.Duration.from('P2D')
// → 2 days
Node.js 26 provides Temporal globally; browser support is still limited.
Async work
Async and await
const value = await a()
const next = await b()
try {
await risky()
} catch (err) {
console.error(err)
}
await new Promise(r =>
setTimeout(r, 1000)
)
Promises
new Promise((resolve, reject) => {
resolve(1)
})
promise
.then(v => v * 2)
.catch(console.error)
await Promise.all([a(), b()])
// → results
await Promise.allSettled([a(), b()])
// → every result, never rejects
await Promise.race([a(), b()])
// → first settled, either way
await Promise.any([a(), b()])
// → first fulfilled
fetch()
const res = await fetch('/data.json')
res.status // → 200
res.ok // → true (200-299)
await res.json() // → parsed body
await fetch('/save', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
Code and syntax
Modules
export const pi = 3.14159
export default class Circle {}
import Circle, { pi } from './math.js'
import * as math from './math.js'
import data from './data.json' with { type: 'json' }
const { default: Chart } = await import('./chart.js')
Modern syntax
const [first, ...rest] = list
const fn = (x = 1) => x * 2
for (const item of list) console.log(item)
Newer APIs
Arrays and objects
[3, 1, 2].toSorted() // → [1, 2, 3]
[1, 2, 3].with(0, 9) // → [9, 2, 3]
const groups = Object.groupBy(list, n => n % 2)
// → { 0: [2], 1: [1, 3] }
Object.hasOwn(user, 'name') // → true
structuredClone(user) // → deep copy
structuredClone is a Web API, not ECMAScript.
See: toSorted · Object.groupBy · structuredClone
Promises and iterators
const { promise, resolve } = Promise.withResolvers()
resolve(42)
await promise // → 42
await Promise.try(() => JSON.parse('{"ok":true}'))
// → { ok: true }
const gen = (async function* () { yield 1; yield 2 })()
await Array.fromAsync(gen) // → [1, 2]
[1, 2].values().map(n => n * 2).toArray()
// → [2, 4]
See: Promise.withResolvers · Array.fromAsync · Iterator helpers
0 Comments for this cheatsheet. Write yours!