ES6 Syntax and Feature Overview
ES6 Syntax and Feature Overview
taniarascia.com/es6-syntax-and-feature-overview
View on GitHub
Note: A commonly accepted practice is to use const except in cases of loops and
reassignment. However, in this resource I'll be using let in place of var for all ES6
examples.
Legend
I'm not a fan of foo bar baz . Here is a key of most identifier names used throughout
this reference.
Variable: x
Object: obj
Array: arr
Function: func
Parameter, method: a , b , c
String: str
Table of contents
Variable declaration
Constant declaration
Arrow function syntax
Template literals
Implicit returns
Key/property shorthand
Method definition shorthand
Destructuring (object matching)
Array iteration (looping)
1/10
Default parameters
Spread syntax
Classes/constructor functions
Inheritance
Modules - export/import
Promises/callbacks
Variable declaration
ES6 introduced the let keyword, which allows for block-scoped variables which cannot
be hoisted or redeclared.
ES5
var x = 0
ES6
let x = 0
Constant declaration
ES6 introduced the const keyword, which cannot be redeclared or reassigned, but is not
immutable.
ES6
Arrow functions
2/10
The arrow function expression syntax is a shorter way of creating a function expression.
Arrow functions do not have their own this , do not have prototypes, cannot be used for
constructors, and should not be used as object methods.
ES5
ES6
Template literals
Concatenation/string interpolation
Expressions can be embedded in template literal strings.
ES5
ES6
Multi-line strings
Using template literal syntax, a JavaScript string can span multiple lines without the need
for concatenation.
ES5
ES6
3/10
Implicit returns
The return keyword is implied and can be omitted if using arrow functions without a
block body.
ES5
function func(a, b, c) {
return a + b + c
}
ES6
Key/property shorthand
ES6 introduces a shorter notation for assigning properties to variables of the same name.
ES5
var obj = {
a: a,
b: b,
}
ES6
let obj = {
a,
b,
}
ES5
var obj = {
a: function (c, d) {},
b: function (e, f) {},
}
ES6
let obj = {
a(c, d) {},
b(e, f) {},
}
4/10
obj.a() // call method a
ES5
var a = obj.a
var b = obj.b
var c = obj.c
ES6
ES5
ES6
Default parameters
Functions can be initialized with default parameters, which will be used only if an
argument is not invoked through the function.
ES5
5/10
ES6
func(10) // returns 12
func(10, 5) // returns 15
Spread syntax
Spread syntax can be used to expand an array.
ES6
ES6
console.log(func(...arr1)) // 6
Classes/constructor functions
ES6 introducess the class syntax on top of the prototype-based constructor function.
ES5
function Func(a, b) {
this.a = a
this.b = b
}
Func.prototype.getSum = function () {
return this.a + this.b
}
ES6
6/10
class Func {
constructor(a, b) {
this.a = a
this.b = b
}
getSum() {
return this.a + this.b
}
}
x.getSum() // returns 7
Inheritance
The extends keyword creates a subclass.
ES5
function Inheritance(a, b, c) {
Func.call(this, a, b)
this.c = c
}
Inheritance.prototype = Object.create(Func.prototype)
Inheritance.prototype.getProduct = function () {
return this.a * this.b * this.c
}
ES6
this.c = c
}
getProduct() {
return this.a * this.b * this.c
}
}
y.getProduct() // 60
Modules - export/import
7/10
Modules can be created to export and import code between files.
index.html
<script src="export.js"></script>
<script type="module" src="import.js"></script>
export.js
import.js
console.log(func(3), obj, x)
Promises/Callbacks
Promises represent the completion of an asynchronous function. They can be used as an
alternative to chaining functions.
ES5 callback
function doSecond() {
console.log('Do second.')
}
function doFirst(callback) {
setTimeout(function () {
console.log('Do first.')
callback()
}, 500)
}
doFirst(doSecond)
ES6 Promise
8/10
let doSecond = () => {
console.log('Do second.')
}
resolve()
}, 500)
})
doFirst.then(doSecond)
An example below using XMLHttpRequest , for demonstrative purposes only (Fetch API
would be the proper modern API to use).
ES5 callback
request.open(method, url)
request.onload = function () {
callback(null, request.response)
}
request.onerror = function () {
callback(request.response)
}
request.send()
}
ES6 Promise
9/10
function makeRequest(method, url) {
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest()
request.open(method, url)
request.onload = resolve
request.onerror = reject
request.send()
})
}
makeRequest('GET', 'https://url.json')
.then((event) => {
console.log(event.target.response)
})
.catch((err) => {
throw new Error(err)
})
MDN Reference
View on GitHub
Comments
10/10