JavaScript
Variables & Types
let x = 1—Block-scoped variable
const y = 2—Block-scoped constant
var z = 3—Function-scoped (avoid)
typeof x—Returns type as string
instanceof—Check prototype chain
undefined—Declared but no value
null—Intentional empty value
NaN—Not a Number result
BigInt(n)—Arbitrary precision integer
Symbol("id")—Unique identifier
String Methods
.length—String character count
.slice(start, end)—Extract substring
.split(sep)—Split into array
.trim()—Remove whitespace ends
.includes(str)—Check if contains string
.replace(a, b)—Replace first match
.replaceAll(a, b)—Replace all matches
.toUpperCase()—Convert to uppercase
.startsWith(str)—Check prefix
`${expr}`—Template literal interpolation
.padStart(n, ch)—Pad string from left
.match(regex)—Regex match result
Array Methods
.push() / .pop()—Add/remove from end
.shift() / .unshift()—Remove/add from start
.map(fn)—Transform each element
.filter(fn)—Keep matching elements
.reduce(fn, init)—Accumulate to single value
.find(fn)—First matching element
.findIndex(fn)—Index of first match
.some(fn) / .every(fn)—Test any/all elements
.forEach(fn)—Iterate (no return)
.sort((a,b) => a-b)—Sort with comparator
.flat(depth)—Flatten nested arrays
[...arr1, ...arr2]—Spread / merge arrays
Objects
{ key: val }—Object literal
obj.key / obj["key"]—Access property
Object.keys(obj)—Array of key names
Object.values(obj)—Array of values
Object.entries(obj)—Array of [key, val] pairs
{ ...obj, key: val }—Spread / merge objects
Object.assign(t, s)—Copy properties to target
delete obj.key—Remove a property
"key" in obj—Check if key exists
Object.freeze(obj)—Make object immutable
Functions
function fn(a, b) {}—Function declaration
const fn = () => {}—Arrow function
(...args) =>—Rest parameters
fn(a, b = 5)—Default parameter value
({ a, b }) =>—Destructured params
return value—Return from function
fn.call(ctx, args)—Call with context
fn.bind(ctx)—Bind this context
IIFE: (() => {})()—Immediately invoked fn
Control Flow
if / else if / else—Conditional branching
switch (val) { case }—Multi-branch matching
for (let i=0; i<n; i++)—Classic for loop
for (const x of arr)—Iterate values
for (const k in obj)—Iterate keys
while (cond) {}—While loop
break / continue—Exit or skip iteration
cond ? a : b—Ternary operator
a ?? b—Nullish coalescing
a?.b?.c—Optional chaining
Async & Promises
new Promise((res, rej))—Create a promise
.then(fn)—Handle resolved value
.catch(fn)—Handle rejection
.finally(fn)—Run after settle
async function fn()—Async function
await promise—Wait for promise result
Promise.all([])—Wait for all promises
Promise.race([])—First settled promise
Promise.allSettled([])—All results (pass or fail)
fetch(url)—HTTP request (returns Promise)
DOM
document.querySelector()—Select first match (CSS)
querySelectorAll()—Select all matches
getElementById()—Select by ID
.addEventListener()—Attach event handler
.textContent—Get/set text
.innerHTML—Get/set HTML content
.classList.add/remove—Manage CSS classes
.setAttribute()—Set HTML attribute
.style.prop—Inline style access
.appendChild()—Insert child element
.remove()—Remove element from DOM
createElement("div")—Create new element
Destructuring & Spread
const { a, b } = obj—Object destructuring
const [x, y] = arr—Array destructuring
const { a: alias } = obj—Rename while destructuring
const { a = 5 } = obj—Default in destructuring
...rest—Collect remaining items
[...new Set(arr)]—Remove duplicates
ES Modules
import x from "mod"—Default import
import { x } from "mod"—Named import
import * as mod—Import all as namespace
export default x—Default export
export { x, y }—Named exports
export const x = 1—Inline named export
allprintabledoc.com