Learn ES2015

es6features

REPL

Introduction

ECMAScript 2015 is an ECMAScript standard that was ratified in June 2015.

ES2015 is a significant update to the language, and the first major update to the language since ES5 was standardized in 2009. Implementation of these features in major JavaScript engines is underway now.

See the ES2015 standard
for full specification of the ECMAScript 2015 language.

ECMAScript 2015 Features

Arrows and Lexical This

Arrows are a function shorthand using the => syntax. They are syntactically
similar to the related feature in C#, Java 8 and CoffeeScript. They support
both expression and statement bodies. Unlike functions, arrows share the same
lexical this as their surrounding code. If an arrow is inside another function,
it shares the "arguments" variable of its parent function.

Classes

ES2015 classes are a simple sugar over the prototype-based OO pattern. Having a
single convenient declarative form makes class patterns easier to use, and
encourages interoperability. Classes support prototype-based inheritance, super
calls, instance and static methods and constructors.

Enhanced Object Literals

Object literals are extended to support setting the prototype at construction,
shorthand for foo: foo assignments, defining methods and making super calls.
Together, these also bring object literals and class declarations closer
together, and let object-based design benefit from some of the same
conveniences.

The __proto__ property requires native support, and was deprecated in previous ECMAScript versions. Most engines now support the property, but some do not. Also, note that only web browsers are required to implement it, as it's in Annex B. It is available in Node.

Template Strings

Template strings provide syntactic sugar for constructing strings. This is
similar to string interpolation features in Perl, Python and more. Optionally, a
tag can be added to allow the string construction to be customized, avoiding
injection attacks or constructing higher level data structures from string
contents.

Destructuring

Destructuring allows binding using pattern matching, with support for matching
arrays and objects. Destructuring is fail-soft, similar to standard object
lookup foo["bar"], producing undefined values when not found.

Default + Rest + Spread

Callee-evaluated default parameter values. Turn an array into consecutive
arguments in a function call. Bind trailing parameters to an array. Rest
replaces the need for arguments and addresses common cases more directly.

Support via polyfill

Generators

Generators simplify iterator-authoring using function* and yield. A function
declared as function* returns a Generator instance. Generators are subtypes of
iterators which include additional next and throw. These enable values to
flow back into the generator, so yield is an expression form which returns a
value (or throws).

Note: Can also be used to enable ‘await’-like async programming, see also ES7 awaitproposal.

Support via polyfill

Comprehensions

Removed in Babel 6.0

Unicode

Non-breaking additions to support full Unicode, including new unicode literal
form in strings and new RegExp u mode to handle code points, as well as new
APIs to process strings at the 21bit code points level. These additions support
building global apps in JavaScript.

Module Formatters

Babel can transpile ES2015 Modules to several different formats including
Common.js, AMD, System, and UMD. You can even create your own. For more
details see the modules docs.

Module Loaders

Not part of ES2015

This is left as implementation-defined within the ECMAScript 2015 specification. The eventual standard will be in WHATWG's Loader specification, but that is currently a work in progress. What is below is from a previous ES2015 draft.

Module loaders support:

Dynamic loading

State isolation

Global namespace isolation

Compilation hooks

Nested virtualization

The default module loader can be configured, and new loaders can be constructed
to evaluate and load code in isolated or constrained contexts.

Unsupported feature

Symbols

Symbols enable access control for object state. Symbols allow properties to be
keyed by either string (as in ES5) or symbol. Symbols are a new primitive
type. Optional name parameter used in debugging - but is not part of identity.
Symbols are unique (like gensym), but not private since they are exposed via
reflection features like Object.getOwnPropertySymbols.

Limited support from polyfill

Most of these APIs are supported by the Babel polyfill. However, certain
features are omitted for various reasons (e.g.
String.prototype.normalize needs a lot of additional code to
support). You can find more polyfills
here.

Binary and Octal Literals

Two new numeric literal forms are added for binary (b) and octal (o).

0b111110111 === 503// true0o767 === 503// true

Only supports literal form

Babel is only able to transform 0o767 and not
Number("0o767").

Promises

Promises are a library for asynchronous programming. Promises are a first class
representation of a value that may be made available in the future. Promises are
used in many existing JavaScript libraries.

Support via polyfill

Reflect API

Full reflection API exposing the runtime-level meta-operations on objects. This
is effectively the inverse of the Proxy API, and allows making calls
corresponding to the same meta-operations as the proxy traps. Especially useful
for implementing proxies.