Although I find the idea of a library in the palm of my hand to be very appealing, I still prefer a paper book to an electronic reader. For this reason, I recently found myself browsing the JavaScript section of a local Barnes & Noble where I discovered Understanding ECMAScript 6 by Nikolas C. Zakas. I picked it up, almost reluctantly, and headed back to a couch. I was not expecting it to be very interesting or useful, but after reading only a few pages, even my wife could tell that I was hooked. She purchased the book as a birthday gift and insisted that I not read any more until my actual birthday. After some begging, I got permission to read it early and thoroughly enjoyed every page.

To get the most out of Understanding ECMAScript 6, a good working knowledge of JavaScript is required. If you don’t have any JavaScript background, this book is not for you. Read JavaScript: The Good Parts instead. If, however, you are generally familiar with the core language concepts like object construction, this usage and behavior, functions as object, closures and prototypes, then you will have a difficult time finding a better resource for learning ES6.

This book is very thorough at 352 pages covering all of the changes from ES5 to ES6 (with the additional changes in ES7 covered in an appendix). Despite its moderate size and level of detail, it is well-organized and reads like a much smaller and lighter book. I found myself flying through the chapters even though there is nothing witty or especially engaging with respect to the writing style or content other than the introduction. This may seem like a contradiction but consider the following excerpt on destructuring.

Destructuring for Easier Data Access

Object and array literals are two of the most frequently used notations in JavaScript, and thanks to the popular JSON data format, they’ve become a particularly important part of the language. It’s quite common to define objects and arrays, and then systematically pull out relevant pieces of information from those structures. ECMAScript 6 simplifies this task by adding destructuring, which is the process of breaking a data structure down into smaller parts. This chapter shows you how to harness destructuring for both objects and arrays.

Why is Destructuring Useful?

In ECMAScript 5 and earlier, the need to fetch information from objects and arrays could lead to a lot of code that looks the same, just to get certain data into local variables. For example:

let options = {
repeat: true,
save: false
};

// extract data from the object
let repeat = options.repeat,
save = options.save;

This code extracts the values of repeat and save from the options object and stores that data in local variables with the same names. While this code looks simple, imagine if you had a large number of variables to assign; you would have to assign them all one by one. And if there was a nested data structure to traverse to find the information instead, you might have to dig through the entire structure just to find one piece of data.

That’s why ECMAScript 6 adds destructuring for both objects and arrays. When you break a data structure into smaller parts, getting the information you need out of it becomes much easier. Many languages implement destructuring with a minimal amount of syntax to make the process simpler to use. The ECMAScript 6 implementation actually makes use of syntax you’re already familiar with: the syntax for object and array literals.

Object Destructuring

Object destructuring syntax uses an object literal on the left side of an assignment operation. For example:

let node = {
type: "Identifier",
name: "foo"
};

let { type, name } = node;

console.log(type); // "Identifier"
console.log(name); // "foo"

In this code, the value of node.type is stored in a variable called type and the value of node.name is stored in a variable called name. This syntax is the same as the object literal property initializer shorthand introduced in Chapter 4. The identifiers type and name are both declarations of local variables and the properties to read the value from on the node object.

In addition to being clear and concise, this excerpt follows a pattern that is repeated methodically throughout the entire book. The pattern is as follows.

  • Summarize a challenge faced by ES5 developers.
  • Briefly describe the ES6 solution to the challenge.
  • Demonstrate the concept with a simple code example.
  • Explain the code in more detail.

After a few concepts have been introduced, this pattern becomes very familiar and the reader is able to quickly digest even the most challenging concepts. By framing every new ES6 feature in the context of a ES5 problem and then demonstrating it with code, Zakas provides an extremely efficient learning experience that engages the reader in a way that even the most entertaining of technical writers cannot. I especially enjoyed the sections on block bindings, arrow functions, classes, promises and modules and would recommend this book as the perfect companion to JavaScript: The Good Parts.