One page

JavaScript cheat sheet

What to keep at hand. If a line is not clear, go back to the matching lesson in the course.

Open the course: JavaScript Essentials

Values

WhatHow
Declareconst total = 0; let count = 0;
Check the typetypeof value // "string" | "number" | ...
An array is an arrayArray.isArray(value)
Empty on purposevalue === null
No valuevalue === undefined
Strict equalitya === b // no type coercion

Strings and numbers

WhatHow
Build text`${item}: ${qty} pcs.`
Trim spacesraw.trim()
Split and jointext.split(" ") parts.join(", ")
Part of a stringtext.slice(0, 3)
Leading zeroString(5).padStart(2, "0") // "05"
String to numberNumber("42") parseInt("42px", 10)
Round for displayvalue.toFixed(2) // a string
Catch NaNNumber.isNaN(value)

Arrays

WhatHow
Lastitems.at(-1)
Transformitems.map((item) => item.title)
Filteritems.filter((item) => item.price > 200)
Find oneitems.find((item) => item.id === id)
Fold into a numberitems.reduce((sum, n) => sum + n, 0)
Copy[...items] items.slice()
Sorting numbers[...items].sort((a, b) => a - b)
No duplicates[...new Set(items)]

Objects

WhatHow
A key from a variableuser[field]
Unpackconst { id, city } = user;
In parametersfunction label({ id, city }) {}
A copy with a change{ ...user, city: "Paris" }
Keys and pairsObject.keys(obj) Object.entries(obj)
Safely deeporder.customer?.city ?? "none"

DOM, events, promises

WhatHow
Finddocument.querySelector("#total")
Find alldocument.querySelectorAll(".row")
Textel.textContent = "1450 ₽"
Classesel.classList.toggle("is-open")
Create and insertlist.appendChild(document.createElement("li"))
Subscribebtn.addEventListener("click", handler)
Formevent.preventDefault(); field.value.trim()
Wait for itconst data = await load();

Other cheat sheets