JS: How to Convert ES5 to ES2015

By Xah Lee. Date: . Last updated: .

Here's a guide to convert ES5 to ES2015.

Follow this checklist, in order.

Variables and Constants

Change var to let

Change some let to const.

in any for-loop, put a let for the counter variable.

Use const as much as possible.

Functions

Remove any use of the arguments object in function.

〔►see JS: Function's “arguments” Object

〔►see JS: Function Parameters

use function default values.

〔►see JS: Function Argument Default Value

use function rest parameter.

〔►see JS: Function Rest Parameters

If a function does not use the this keyword, change it to arrow function.

〔►see JS: “this” Binding

〔►see JS: Arrow Function

Change Function Blocks into Code Blocks

Remove the outmost function block of the from (function (){…})() that are just for creating local variables. Replace it with {…} and let.

replace all function f () {…} form to const f = function () {…}

You have to do this. Because we want to wrap a {…} around our code. If you don't change function declaration into assignment form, you can't wrap the whole code into a block because function declaration must happen only at top level.

Now, wrap whole file into a block by wrapping curly brackets {} around it.

Dealing with Unicode

Replace String.fromCharCode to String.fromCodePoint

〔►see JS: String.fromCharCode

〔►see JS: String.fromCodePoint

Replace charCodeAt to codePointAt

〔►see JS: String.prototype.charCodeAt

〔►see JS: String.prototype.codePointAt

Use Map

Rewrite some data structure that use object, to use Map.

〔►see JS: the Map Object Tutorial

Use Set

Rewrite some data structure that use array or object, to use Set.

〔►see JS: the Set Object Tutorial

Use Class

Rewrite constructor functions to use the class keyword. This would be a big complex change. Better is to change the code to functional programing style.

〔►see JS: Class Tutorial

Like what you read? Buy JavaScript in Depth