Chapters: JavaScript HTML & CSS JSON XML YAML Go Java ← Full TOC

← JavaScript Index

⚡ ES6+ Core — Modern JavaScript — let/const, arrow functions, destructuring, spread

ES6+ introduced fundamental improvements to JavaScript: block scoping, arrow functions, template literals, destructuring, default parameters, spread/rest operators, and module syntax. These features are now standard in all modern browsers and Node.js.

📋 Methods & APIs

NameSignature / SyntaxDescription
let / constlet x = 1; const PI = 3.14Block-scoped variable / immutable binding
Arrow function(a, b) => a + bConcise function, lexically binds this
Template literal`Hello ${name}!`String interpolation with backticks
Destructuring arrayconst [a, b] = [1, 2]Unpack array values into variables
Destructuring objectconst {name, age} = personUnpack object properties
Default paramsfunction f(x = 10) {}Parameter default values
Spread operator[...arr1, ...arr2]Expand iterable into elements
Rest paramsfunction f(...args) {}Collect remaining args into array
Optional chainingobj?.prop?.subSafe navigation — returns undefined if null
Nullish coalescingval ?? 'default'Return right side only if left is null/undefined
Logical assignmentx ||= y; x &&= y; x ??= yAssign only under logical condition
Object shorthandconst obj = {name, age}Property shorthand when key = variable name
Computed propertiesconst obj = {[key]: value}Dynamic property key
for...offor (const x of iterable) {}Iterate over iterable values
for...infor (const key in obj) {}Iterate over object keys
SymbolSymbol('desc')Unique, immutable primitive value
typeoftypeof x → stringReturn type as string
instanceofx instanceof ArrayTrue if x is instance of constructor
classclass Animal { constructor() {} }ES6 class syntax (syntactic sugar over prototype)
extendsclass Dog extends Animal {}Inheritance
supersuper(args) / super.method()Call parent constructor or method
staticstatic method() {}Class-level method (not on instances)
getter/setterget name() {} / set name(v) {}Computed property access
importimport { fn } from './module.js'ES module import
exportexport default fn / export { fn }ES module export
Proxynew Proxy(target, handler)Intercept object operations
ReflectReflect.get/set/apply(...)Mirrors Proxy traps as functions
WeakMapnew WeakMap()Map with weakly-held object keys (no memory leak)
WeakSetnew WeakSet()Set with weakly-held object members
globalThisglobalThis.fetchUniversal global object reference

💡 Example

Run with node script.js or save as HTML and open in browser.

// ES6+ Modern JavaScript
// --- Destructuring & defaults ---
const [first, second = 'default', ...rest] = ['a', undefined, 'c', 'd'];
console.log(first, second, rest);  // a default ['c','d']

const { name, age = 25, address: { city } = {} } = { name: 'Alice', address: { city: 'LA' } };
console.log(name, age, city);  // Alice 25 LA

// --- Arrow functions & closures ---
const multiply = (x, y) => x * y;
const makeCounter = (start = 0) => {
  let count = start;
  return { inc: () => ++count, dec: () => --count, val: () => count };
};
const counter = makeCounter(10);
console.log(counter.inc(), counter.inc(), counter.dec());  // 11 12 11

// --- Template literals ---
const greet = (name, score) =>
  `Welcome, ${name.toUpperCase()}! Score: ${score.toFixed(1)} (${score >= 90 ? 'A' : 'B'})`;
console.log(greet('alice', 94.7));

// --- Spread & rest ---
const arr1 = [1, 2, 3], arr2 = [4, 5, 6];
const merged = [...arr1, ...arr2];
console.log(Math.max(...merged));  // 6

const sumAll = (...nums) => nums.reduce((a, b) => a + b, 0);
console.log(sumAll(1, 2, 3, 4, 5));  // 15

// --- Optional chaining & nullish coalescing ---
const user = { profile: { theme: null } };
const theme = user?.profile?.theme ?? 'dark';
console.log(theme);  // dark

// --- Classes ---
class Shape {
  constructor(color = 'black') { this.color = color; }
  area() { return 0; }
  toString() { return \`\${this.constructor.name}(color=\${this.color}, area=\${this.area().toFixed(2)})\`; }
}
class Circle extends Shape {
  constructor(radius, color) { super(color); this.radius = radius; }
  area() { return Math.PI * this.radius ** 2; }
}
class Rectangle extends Shape {
  constructor(w, h, color) { super(color); this.w = w; this.h = h; }
  area() { return this.w * this.h; }
}
const shapes = [new Circle(5,'red'), new Rectangle(4,6,'blue'), new Circle(2.5,'green')];
shapes.forEach(s => console.log(s.toString()));
// node script.js

🏠 Index  |  Arrays & Iteration →