ES2015 (ES6): The Big Update
ES2015, widely called ES6, was the largest update in JavaScript's history: six years in the making, it added more features than all previous editions combined and turned JavaScript into the language most people write today. This lesson is a tour, grouped by purpose: for each feature, a short example and the course lesson that teaches it in depth. Use it to recognize the modern style, and as a checklist when updating older code.
In One Minute
- Variables and scope:
let,const, block scope. - Functions: arrow functions, default and rest parameters, spread.
- Syntax: template literals, destructuring, shorthand and computed properties,
for...of. - Classes and modules:
class,extends,super,import/export. - Built-ins: promises,
Map,Set,WeakMap,WeakSet, symbols, iterators and generators,Proxy,Reflect, new string, number, array and object methods.
Variables and Scope
const rate = 2;
let total = 0;
for (let day = 1; day <= 3; day++) total += rate;
console.log(total, typeof day);6 undefinedconst for values that aren't reassigned, let for those that are, both block-scoped, ending the era of var hoisting surprises. See "Variables: Let and Const" and "Var, Let and Const Compared".
Functions
const lateFee = (days, rate = 2, ...extra) => Math.min(days * rate, 30) + extra.length;
console.log(lateFee(5), lateFee(20, 3), lateFee(1, 2, "a", "b"));
const fees = [120, 85, 45];
console.log(Math.max(...fees), [...fees, 200]);10 30 4
120 [120, 85, 45, 200]| Feature | Lesson |
|---|---|
Arrow functions (and lexical this) | "Arrow Functions" |
| Default parameters, rest parameters | "Parameters and Arguments" |
| Spread in calls and arrays | "Destructuring and Spread" |
Syntax Conveniences
const name = "Srihitha";
const grade = 11;
const field = "balance";
const student = { name, grade, [field]: 85, describe() { return `${this.name}, grade ${this.grade}`; } };
const { balance, ...rest } = student;
const [first, , third] = ["Navika", "Srihitha", "Advitha"];
console.log(student.describe(), balance, Object.keys(rest));
console.log(first, third);
for (const letter of "Fee") console.log(letter);Srihitha, grade 11 85 ['name', 'grade', 'describe']
Navika Advitha
F
e
e| Feature | Lesson |
|---|---|
| Template literals (and tagged templates) | "Template Literals" |
| Destructuring | "Destructuring and Spread" |
| Shorthand properties, computed keys, method shorthand | "Objects", "Object Methods and This" |
for...of | "The for...of and for...in Loops" |
(Object rest and spread, { ...rest }, actually arrived in ES2018; array destructuring and rest were ES2015.)
Classes and Modules
class Payment {
constructor(amount) { this.amount = amount; }
get label() { return `$${this.amount}`; }
static zero() { return new Payment(0); }
}
class CardPayment extends Payment {
get label() { return `${super.label} by card`; }
}
console.log(new CardPayment(120).label, Payment.zero().label);$120 by card $0Classes gave JavaScript a clear syntax for constructors, methods, getters, static members and inheritance (see "Classes" and "Class Inheritance"). Modules (import and export) gave it a standard way to split code into files ("ES Modules"). Private fields and class fields came later (ES2022).
Promises
const wait = (ms, value) => new Promise((resolve) => setTimeout(() => resolve(value), ms));
wait(10, 120)
.then((amount) => amount + 30)
.then((total) => console.log("total:", total));
Promise.all([wait(5, "a"), wait(1, "b")]).then((values) => console.log(values));['a', 'b']
total: 150Promises standardized asynchronous results, replacing callback pyramids, and laid the foundation for async/await (ES2017). See "Promises".
New Collections and Symbols
const balances = new Map([["Navika", 120]]);
balances.set("Advitha", 45);
const grades = new Set([9, 11, 9]);
const id = Symbol("id");
console.log(balances.get("Advitha"), balances.size, [...grades]);
console.log(typeof id, { [id]: 1, visible: 2 });45 2 [9, 11]
symbol { visible: 2 }| Feature | Lesson |
|---|---|
Map, Set | "Maps", "Sets" |
WeakMap, WeakSet | "WeakMap and WeakSet" |
Symbol, well-known symbols | "Symbols" |
Iterators and Generators
function* receiptNumbers(start) {
let n = start;
while (true) yield `R-${n++}`;
}
const gen = receiptNumbers(1001);
console.log(gen.next().value, gen.next().value);
const schedule = { *[Symbol.iterator]() { yield 400; yield 415; } };
console.log([...schedule]);R-1001 R-1002
[400, 415]The iteration protocol made for...of, spread and destructuring work on any data source. See "Iterables and Iterators" and "Generators".
Meta-Programming
Proxy and Reflect let code intercept and forward fundamental object operations (see "Proxy and Reflect").
New Methods on Built-Ins
| Object | ES2015 additions |
|---|---|
| String | includes, startsWith, endsWith, repeat, codePointAt, String.raw |
| Number | Number.isInteger, isFinite, isNaN, isSafeInteger, EPSILON, MAX_SAFE_INTEGER |
| Math | trunc, sign, cbrt, hypot, log2, log10... |
| Array | Array.from, Array.of, find, findIndex, fill, keys, values, entries, copyWithin (includes came a year later, in ES2016) |
| Object | Object.assign, Object.is, Object.setPrototypeOf, Object.getOwnPropertySymbols |
| RegExp | The u (Unicode) and y (sticky) flags |
console.log("Fee Tracker".startsWith("Fee"), "ab".repeat(3), Number.isInteger(5.0));
console.log(Array.from("abc"), [120, 85, 45].find((n) => n < 100), Object.is(NaN, NaN));
console.log(Object.assign({}, { a: 1 }, { b: 2 }), Math.trunc(-4.7));true ababab true
['a', 'b', 'c'] 85 true
{ a: 1, b: 2 } -4Why It Took Until 2015
After ES3 (1999), an ambitious ES4 failed in 2008, and ES5 (2009) was a smaller, compatible update. ES2015 (developed under the name "Harmony") collected years of ideas into one release. The committee then decided never to do a giant release again, and switched to small yearly editions so features could ship as soon as they were ready.
In Practice
A pre-2015 Fee Tracker module, converted feature by feature:
class Account {
constructor(owner, payments = []) {
this.owner = owner;
this.payments = payments;
}
get total() {
return this.payments.reduce((sum, p) => sum + p, 0);
}
summary() {
return `${this.owner} paid $${this.total} in ${this.payments.length} payments`;
}
}
const accounts = [new Account("Navika", [120, 60]), new Account("Advitha")];
for (const account of accounts) console.log(account.summary());function Account(owner, payments) {
this.owner = owner;
this.payments = payments || [];
}
Account.prototype.total = function () {
var sum = 0;
for (var i = 0; i < this.payments.length; i++) sum += this.payments[i];
return sum;
};
Account.prototype.summary = function () {
return this.owner + " paid $" + this.total() + " in " + this.payments.length + " payments";
};
var accounts = [new Account("Navika", [120, 60]), new Account("Advitha")];
for (var i = 0; i < accounts.length; i++) console.log(accounts[i].summary());Navika paid $180 in 2 payments
Advitha paid $0 in 0 paymentsCommon Mistakes
Mixing var and let in the same code. Convert consistently; var keeps its function-scope surprises.
Using arrows as methods when converting. Methods that use this should use method shorthand.
Treating class as a new object model. It's syntax over prototypes (see "Prototypes").
Assuming every modern feature is ES2015. Object spread (ES2018), async (ES2017) and class fields (ES2022) came later.
Practice
Challenge: Use at least five ES2015 features (arrow function, default parameter, template literal, destructuring, Map) to build a function that groups payments by student and prints totals.
Show solution
const groupTotals = (payments, currency = "USD") => {
const totals = new Map();
for (const { name, amount } of payments) totals.set(name, (totals.get(name) || 0) + amount);
return [...totals].map(([name, total]) => `${name}: ${total} ${currency}`);
};
console.log(groupTotals([{ name: "Navika", amount: 120 }, { name: "Advitha", amount: 45 }, { name: "Navika", amount: 60 }]));['Navika: 180 USD', 'Advitha: 45 USD']Key Takeaways
| Group | Highlights |
|---|---|
| Scope | let, const |
| Functions | Arrows, defaults, rest, spread |
| Syntax | Template literals, destructuring, for...of |
| Structure | class, modules |
| Async | Promises |
| Data | Map, Set, symbols, iterators |
Was this tutorial helpful?