A brief overview of some of the latest features available in ECMAScript 6, with examples of how they were written previously in ECMAScript 5, if even possible.
Constants
Constants are immutable variables or variables which cannot be re-assigned new content.
1
constPI = 3.141593
Scoping
Block-Scoped Variables
ECMAScript 6:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
for (let i = 0; i < a.length; i++) { let x = a[i]; … } for (let i = 0; i < b.length; i++) { let y = b[i]; … }
let callbacks = []; for (let i = 0; i <= 2; i++) { callbacks[i] = function () { return i * 2; }; } callbacks[0]() === 0; callbacks[1]() === 2; callbacks[2]() === 4;
ECMAScript 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var i, x, y; for (i = 0; i < a.length; i++) { x = a[i]; … } for (i = 0; i < b.length; i++) { y = b[i]; … }
var callbacks = []; for (var i = 0; i <= 2; i++) { (function (i) { callbacks[i] = function() { return i * 2; }; })(i); } callbacks[0]() === 0; callbacks[1]() === 2; callbacks[2]() === 4;
// only in ES5 with the help of block-scope emulating // function scopes and function expressions (function () { var blah = function () { return1; } blah() === 1; (function () { var blah = function () { return2; } blah() === 2; })(); blah() === 1; })();
Arrow Functions
Expression Bodies
More expressive closure syntax.
ECMAScript 6:
1 2 3
odds = evens.map(v => v + 1); pairs = evens.map(v => ({ even: v, odd: v + 1 })); nums = evens.map((v, i) => v + i);
nums.forEach(v => { if (v % 5 === 0) fives.push(v); })
ECMAScript 5:
1 2 3 4
nums.forEach(function (v) { if (v % 5 === 0) fives.push(v); });
Lexical this
More intuitive handling of current object context.
ECMAScript 6:
1 2 3 4
this.nums.forEach((v) => { if (v % 5 === 0) this.fives.push(v); });
ECMAScript 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// variant 1 var self = this; this.nums.forEach(function (v) { if (v % 5 === 0) self.fives.push(v); });
// variant 2 this.nums.forEach(function (v) { if (v % 5 === 0) this.fives.push(v); }, this);
// variant 3 (since ECMAScript 5.1 only) this.nums.forEach(function (v) { if (v % 5 === 0) this.fives.push(v); }.bind(this));
Extended Parameter Handling
Default Parameter Values
Simple and intuitive default values for function parameters.
ECMAScript 6:
1 2 3 4
functionblah (x, y = 7, z = 42) { return x + y + z; } blah(1) === 50;
ECMAScript 5:
1 2 3 4 5 6 7 8
functionblah (x, y, z) { if (y === undefined) y = 7; if (z === undefined) z = 42; return x + y + z; }; blah(1) === 50;
Rest Parameter
Aggregation of remaining arguments into single parameter of variadic functions (a function where the total number of parameters are unknown and can be adjusted at the time the method is called).
var str = "blah"; var chars = str.split(""); // [ "f", "o", "o" ]
Template Literals
String Interpolation
Intuitive expression interpolation for single-line and multi-line strings.
ECMAScript 6:
1 2 3 4 5
var customer = { name: "Foo" }; var card = { amount: 7, product: "Bar", unitprice: 42 }; var message = `Hello ${customer.name}, want to buy ${card.amount}${card.product} for a total of ${card.amount * card.unitprice} bucks?`;
ECMAScript 5:
1 2 3 4 5
var customer = { name: "Foo" }; var card = { amount: 7, product: "Bar", unitprice: 42 }; var message = "Hello " + customer.name + ",\n" + "want to buy " + card.amount + " " + card.product + " for\n" + "a total of " + (card.amount * card.unitprice) + " bucks?";
Custom Interpolation
Flexible expression interpolation for arbitrary methods.
obj = { foo: function (a, b) { ... }, bar: function (x, y) { ... }, // quux: no equivalent in ES5 }
Destructuring Assignment
Destructuring allows you to assign the properties of an object or array to variables using syntax that looks similar to object or array literals. This syntax can be extremely terse, while still exhibiting more clarity than the traditional property access.
Array Matching
Intuitive and flexible destructuring of Arrays into individual variables during assignment.
ECMAScript 6:
1 2 3
var list = [ 1, 2, 3 ] var [ a, , b ] = list [ b, a ] = [ a, b ]
ECMAScript 5:
1 2 3
var list = [ 1, 2, 3 ]; var a = list[0], b = list[2]; var tmp = a; a = b; b = tmp;
Object Matching, Shorthand
Intuitive and flexible destructuring of Objects into individual variables during assignment.
AST = Abstract Syntax Tree, which are data structures used in compilers. Further reading
ECMAScript 6:
1
var { op, lhs, rhs } = getASTNode()
ECMAScript 5:
1 2 3 4
var tmp = getASTNode(); var op = tmp.op; var lhs = tmp.lhs; var rhs = tmp.rhs;
Object Matching, Deep Matching
Intuitive and flexibile destructuring of Objects into individual variables during assignment.
ECMAScript 6:
1
var { op: a, lhs: { op: b }, rhs: c } = getASTNode()
ECMAScript 5:
1 2 3 4
var tmp = getASTNode(); var op = tmp.op; var lhs = tmp.lhs; var rhs = tmp.rhs;
Object and Array Matching, Default Values
ECMAScript 6:
1 2 3 4
var obj = { a: 1 } var list = [ 1 ] var { a, b = 2 } = obj var [ x, y = 2 ] = list
ECMAScript 5:
1 2 3 4 5 6
var obj = { a: 1 }; var list = [ 1 ]; var a = obj.a; var b = obj === undefined ? 2 : obj.b; var x = list[0]; var y = list[1] === undefined ? 2 : list[1];
Parameter Context Matching
Intuitive and flexible destructuring of Arrays and Objects into individual parameters during function calls.
ECMAScript 6:
1 2 3 4 5 6 7 8 9 10 11 12 13
functionf ([ name, val ]) { console.log(name, val) } functiong ({ name: n, val: v }) { console.log(n, v) } functionh ({ name, val }) { console.log(name, val) }
functionf (arg) { var name = arg[0]; var val = arg[1]; console.log(name, val); } functiong (arg) { var n = arg.name; var v = arg.val; console.log(n, v); } functionh (arg) { var name = arg.name; var val = arg.val; console.log(n, v); } f([ "bar", 42 ]); g({ name: "foo", val: 7 }); h({ name: "bar", val: 42 });
Fail-Soft Destructuring
Fail-soft destructuring, optionally with defaults
ECMAScript 6:
1 2 3 4 5 6
var list = [ 7, 42 ]; var [ a = 1, b = 2, c = 3, d ] = list a === 7 b === 42 c === 3 d === undefined
ECMAScript 5:
1 2 3 4 5 6 7 8 9
var list = [ 7, 42 ]; var a = typeof list[0] !== "undefined" ? list[0] : 1; var b = typeof list[1] !== "undefined" ? list[1] : 2; var c = typeof list[2] !== "undefined" ? list[2] : 3; var d = typeof list[3] !== "undefined" ? list[3] : undefined; a === 7; b === 42; c === 3; d === undefined;
Modules
Value Export/Import
Support for exporting/importing values from/to modules without global namespace pollution.
ECMAScript 6:
1 2 3 4 5 6 7 8 9 10 11
// lib/math.js exportfunctionsum (x, y) { return x + y } exportvar pi = 3.141593
// someApp.js import * as math from"lib/math.js" console.log("2π = " + math.sum(math.pi, math.pi))
Support for mixin-style inheritance by extending from expressions yielding function objects. The generic aggregation function is usually provided by a library such as this.
classColored { initializer () { this._color = "white" } get color () { returnthis._color } set color (v) { this._color = v } }
classZCoord { initializer () { this._z = 0 } get z () { returnthis._z } set z (v) { this._z = v } }
classShape { constructor (x, y) { this._x = x; this._y = y } get x () { returnthis._x } set x (v) { this._x = v } get y () { returnthis._y } set y (v) { this._y = v } }