Part 29 — Cheat Sheets
Truthy / Falsy, Equality quick table, Array methods — mutate vs return-new, String methods, Object methods
Truthy / Falsy#
Falsy (all 8): false, 0, -0, 0n, "", null, undefined, NaN. Everything else truthy (incl. "0", "false", [], {}).
Equality quick table#
| Expr | Result |
|---|---|
0 == "0" |
true |
0 == "" |
true |
0 == false |
true |
null == undefined |
true |
null == 0 |
false |
NaN === NaN |
false |
Object.is(NaN, NaN) |
true |
[] == ![] |
true |
Array methods — mutate vs return-new#
Mutate: push pop shift unshift splice sort reverse fill copyWithin.
Return new / non-mutating: map filter reduce slice concat flat flatMap find findIndex some every includes indexOf join at toSorted toReversed toSpliced with.
String methods#
length at slice substring toUpperCase toLowerCase trim includes startsWith endsWith indexOf replace replaceAll split padStart padEnd repeat match matchAll normalize codePointAt localeCompare.
Object methods#
keys values entries fromEntries assign create freeze seal preventExtensions hasOwn getOwnPropertyNames getPrototypeOf setPrototypeOf defineProperty groupBy · structuredClone for deep clone.
Promise APIs#
then catch finally · Promise.resolve/reject · Promise.all all any(→ AggregateError) race allSettled · Promise.withResolvers · async/await.
Loops#
for · while · do…while · for…of (values) · for…in (keys, incl. inherited) · for await…of (async). forEach/map/filter/reduce — no break, ignore await.
this rules#
new > call/apply/bind > obj.method() > default(undefined/global); arrows = lexical.
Time complexity (arrays)#
index O(1) · push/pop O(1) · shift/unshift O(n) · splice O(n) · search O(n) · sort O(n log n).
Script loading#
defer = parse-then-run, ordered, non-blocking (🟢). async = run-when-ready, unordered. modules = deferred.
DOM essentials#
querySelector(All) · createElement · append/prepend/remove · textContent (safe) vs innerHTML (XSS) · addEventListener(type, fn, {capture,once,passive}) · e.target/currentTarget · e.preventDefault()/stopPropagation().
Conversions#
Number() parseInt(str, radix) parseFloat() String() Boolean() / !! Array.from() JSON.parse/stringify (numbers).toString(radix).