Variables
const x = 1 | Block-scoped constant |
let x = 1 | Block-scoped variable |
var x = 1 | Function-scoped (avoid) |
Arrow Functions
const add = (a, b) => a + b | Concise arrow function |
const fn = x => x * 2 | Single param, implicit return |
const fn = () => ({ key: val }) | Return object literal |
Destructuring
const { a, b } = obj | Object destructuring |
const { a: alias } = obj | Rename while destructuring |
const { a = 10 } = obj | Default value |
const [x, y] = arr | Array destructuring |
const [first, ...rest] = arr | Rest in array |
Spread & Rest
const merged = { ...a, ...b } | Merge objects |
const arr2 = [...arr1, 4, 5] | Spread array |
function f(a, ...rest) {} | Rest parameters |
Template Literals
`Hello ${name}!` | String interpolation |
`multi
line
string` | Multi-line string |
Classes
class Animal {
#name; // private
constructor(n) { this.#name = n; }
speak() {}
} | Class with private field |
class Dog extends Animal { } | Inheritance |
static create() {} | Static method |
Modules
export const x = 1 | Named export |
export default class {} | Default export |
import { x } from './mod.js' | Named import |
import MyClass from './mod.js' | Default import |
import * as mod from './mod.js' | Namespace import |