let x = 1 | Block-scoped variable |
const y = 2 | Constant (cannot reassign) |
var z = 3 | Function-scoped (avoid) |
typeof x | Get type of variable |
undefined | Uninitialized variable |
null | Intentional empty value |
NaN | Not a Number |
Infinity / -Infinity | Infinite values |
=== / !== | Strict equality (type + value) |
== / != | Loose equality (avoid) |
&& | Logical AND |
|| | Logical OR |
?? | Nullish coalescing |
?. | Optional chaining |
...arr | Spread operator |
...rest | Rest parameters |
** | Exponentiation |
`Hello ${name}` | Template literal |
s.length | String length |
s.toUpperCase() / toLowerCase() | Change case |
s.trim() / trimStart() / trimEnd() | Remove whitespace |
s.split(sep) | Split to array |
s.slice(start, end) | Extract substring |
s.substring(start, end) | Extract substring |
s.replace(old, new) | Replace first match |
s.replaceAll(old, new) | Replace all matches |
s.includes(sub) | Check if contains |
s.startsWith(pre) / endsWith(suf) | Check start/end |
s.indexOf(sub) | Find index (-1 if not found) |
s.padStart(len, char) / padEnd() | Pad string |
s.repeat(n) | Repeat string |
[...arr1, ...arr2] | Merge arrays |
arr.push(x) / pop() | Add/remove from end |
arr.unshift(x) / shift() | Add/remove from start |
arr.slice(start, end) | Extract portion (immutable) |
arr.splice(i, n, ...items) | Remove/insert (mutates) |
arr.concat(arr2) | Merge arrays |
arr.join(sep) | Join to string |
arr.reverse() | Reverse (mutates) |
arr.sort((a,b) => a-b) | Sort (mutates) |
arr.includes(x) | Check if contains |
arr.indexOf(x) / lastIndexOf(x) | Find index |
arr.flat(depth) | Flatten nested arrays |
Array.isArray(x) | Check if array |
Array.from(iterable) | Create from iterable |
arr.forEach(fn) | Execute for each element |
arr.map(fn) | Transform elements |
arr.filter(fn) | Filter elements |
arr.reduce((acc, x) => ..., init) | Reduce to single value |
arr.find(fn) | Find first match |
arr.findIndex(fn) | Find index of first match |
arr.some(fn) | Test if any matches |
arr.every(fn) | Test if all match |
arr.flatMap(fn) | Map then flatten |
{ ...obj1, ...obj2 } | Merge objects |
{ key: value } | Object literal |
{ key } | Shorthand property |
{ [expr]: value } | Computed property |
obj.key / obj["key"] | Access property |
obj?.key | Optional chaining |
delete obj.key | Delete property |
"key" in obj | Check if key exists |
Object.keys(obj) | Get all keys |
Object.values(obj) | Get all values |
Object.entries(obj) | Get [key, value] pairs |
Object.assign(target, src) | Copy properties |
Object.freeze(obj) | Make immutable |
Object.fromEntries(arr) | Create from entries |
const { a, b } = obj | Object destructuring |
const { a: x } = obj | Rename variable |
const { a = 1 } = obj | Default value |
const { a, ...rest } = obj | Rest properties |
const [a, b] = arr | Array destructuring |
const [a, , b] = arr | Skip elements |
const [a, ...rest] = arr | Rest elements |
function name(a, b) {} | Function declaration |
const fn = function() {} | Function expression |
const fn = () => {} | Arrow function |
const fn = (a) => a * 2 | Arrow with implicit return |
function fn(a = 1) {} | Default parameter |
function fn(...args) {} | Rest parameters |
fn.call(this, a, b) | Call with this context |
fn.apply(this, [a, b]) | Call with array args |
fn.bind(this) | Create bound function |
let (block scope) | Block-scoped variable |
const (block scope) | Block-scoped constant |
var (function scope) | Function-scoped (hoisted) |
closure | Function accessing outer scope |
IIFE: (function() {})() | Immediately invoked |
new Promise((resolve, reject) => {}) | Create promise |
promise.then(fn) | Handle fulfillment |
promise.catch(fn) | Handle rejection |
promise.finally(fn) | Always execute |
Promise.resolve(val) | Create resolved promise |
Promise.reject(err) | Create rejected promise |
Promise.all([p1, p2]) | Wait for all (fail-fast) |
Promise.allSettled([p1, p2]) | Wait for all (no fail) |
Promise.race([p1, p2]) | First to settle |
Promise.any([p1, p2]) | First to fulfill |
async function fn() {} | Async function |
const fn = async () => {} | Async arrow function |
await promise | Wait for promise |
try { await } catch (e) {} | Handle errors |
await Promise.all([...]) | Parallel execution |
for await (const x of asyncIter) | Async iteration |
// Async/await with error handling
async function fetchData(url) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error('HTTP error');
const data = await response.json();
return data;
} catch (error) {
console.error('Fetch failed:', error);
throw error;
}
}
// Parallel requests
const [users, posts] = await Promise.all([
fetchData('/api/users'),
fetchData('/api/posts')
]); class Name {} | Class declaration |
class Child extends Parent {} | Inheritance |
constructor() {} | Constructor method |
super() | Call parent constructor |
super.method() | Call parent method |
static method() {} | Static method |
static property = value | Static property |
get prop() {} | Getter |
set prop(val) {} | Setter |
#privateField | Private field |
#privateMethod() {} | Private method |
export const x = 1 | Named export |
export default fn | Default export |
export { x, y } | Export list |
export { x as alias } | Export with rename |
import { x } from './mod' | Named import |
import x from './mod' | Default import |
import * as mod from './mod' | Namespace import |
import { x as alias } from './mod' | Import with rename |
import('./mod') | Dynamic import |
export * from './mod' | Re-export all |
document.getElementById(id) | Get by ID |
document.querySelector(sel) | Get first match |
document.querySelectorAll(sel) | Get all matches |
document.getElementsByClassName(c) | Get by class |
document.getElementsByTagName(t) | Get by tag |
el.closest(sel) | Find closest ancestor |
el.matches(sel) | Test if matches selector |
el.textContent = "text" | Set text content |
el.innerHTML = "<p>html</p>" | Set HTML content |
el.setAttribute(name, val) | Set attribute |
el.getAttribute(name) | Get attribute |
el.classList.add(c) / remove(c) | Add/remove class |
el.classList.toggle(c) | Toggle class |
el.classList.contains(c) | Check class |
el.style.prop = value | Set inline style |
document.createElement(tag) | Create element |
parent.appendChild(el) | Append child |
parent.removeChild(el) | Remove child |
el.remove() | Remove element |
el.addEventListener(type, fn) | Add event listener |
el.removeEventListener(type, fn) | Remove event listener |
event.preventDefault() | Prevent default action |
event.stopPropagation() | Stop bubbling |
event.target | Element that triggered |
event.currentTarget | Element with listener |
{ once: true, passive: true } | Event options |
const by default, let when needed, avoid var=== instead of == for comparisonsthisasync/await instead of .then() chains?. to avoid null errors?? for default values