JavaScript ES6+ Reference
Modern JavaScript ES6+ features including arrow functions, destructuring, and more
Arrow Functions
# Traditional function
function add(a, b) { return a + b; }
# Arrow function
const add = (a, b) => a + b; # implicit return
const square = x => x * x; # single param
const greet = () => "Hello"; # no params
Destructuring
# Array destructuring
const [a, b] = [1, 2]; # a=1, b=2
const [x, , z] = [1, 2, 3]; # skip middle
# Object destructuring
const {name, age} = user; # extract properties
const {name: userName} = user; # rename variable
Spread & Rest
# Spread operator
const arr = [...arr1, ...arr2]; # merge arrays
const obj = {...obj1, ...obj2}; # merge objects
# Rest parameters
function sum(...numbers) { # collect args
return numbers.reduce((a, b) => a + b);
}
Template Literals
const name = "John";
const age = 30;
const msg = `Hello ${name}, you are ${age}`; # interpolation
# Multi-line strings
const html = `
${title}
`;
Let, Const & Classes
let x = 10; # block scoped, reassignable
const y = 20; # block scoped, constant
# ES6 Classes
class Person {
constructor(name) {
this.name = name;
}
greet() {
return `Hello ${this.name}`;
}
}