JavaScript

变量与类型

let x = 1Block-scoped variable
const y = 2Block-scoped constant
var z = 3Function-scoped (avoid)
typeof xReturns type as string
instanceofCheck prototype chain
undefinedDeclared but no value
nullIntentional empty value
NaNNot a Number result
BigInt(n)Arbitrary precision integer
Symbol("id")Unique identifier

数组

.lengthString 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

对象

.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

函数

{ 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.keyRemove a property
"key" in objCheck if key exists
Object.freeze(obj)Make object immutable

DOM操作

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 valueReturn from function
fn.call(ctx, args)Call with context
fn.bind(ctx)Bind this context
IIFE: (() => {})()Immediately invoked fn

Control Flow

if / else if / elseConditional 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 / continueExit or skip iteration
cond ? a : bTernary operator
a ?? bNullish coalescing
a?.b?.cOptional 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 promiseWait 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
.textContentGet/set text
.innerHTMLGet/set HTML content
.classList.add/removeManage CSS classes
.setAttribute()Set HTML attribute
.style.propInline style access
.appendChild()Insert child element
.remove()Remove element from DOM
createElement("div")Create new element

Destructuring & Spread

const { a, b } = objObject destructuring
const [x, y] = arrArray destructuring
const { a: alias } = objRename while destructuring
const { a = 5 } = objDefault in destructuring
...restCollect remaining items
[...new Set(arr)]Remove duplicates

ES Modules

import x from "mod"Default import
import { x } from "mod"Named import
import * as modImport all as namespace
export default xDefault export
export { x, y }Named exports
export const x = 1Inline named export
allprintabledoc.com