Appendix E. ES6 for success

 

This appendix provides a quick introduction to ES6. It describes the 10 best features of the new generation of the most popular programming language—JavaScript:

  1. Default parameters
  2. Template literals
  3. Multiline strings
  4. Destructuring assignment
  5. Enhanced object literals
  6. Arrow functions
  7. Promises
  8. Block-scoped constructs: let and const
  9. Classes
  10. Modules
Note

This list if highly subjective. It’s in no way intended to diminish the usefulness of other ES6 features that didn’t make it on the list only because I wanted to limit the number to 10.

Default parameters

Remember when we had to use statements like these to define default parameters?

var link = function (height, color, url) {
    var height = height || 50
    var color = color || 'red'
    var url = url || 'http://azat.co'
    ...
}
Print-ready PDF available

In addition to this essay, I’ve created a free beautifully designed, print-ready ES6/ES2015 cheatsheet. You can request this PDF at http://reactquickly.co/resources.

This approach was fine until the value wasn’t 0. When you have 0, there may be a bug. A 0 value defaults to the hardcoded value instead of becoming the value itself, because 0 is falsy in JavaScript. Of course, who needs 0 as a value (#sarcasmfont)? So we ignored this flaw and used the logical OR. No more! In ES6, you can put the default value right in the signature of the function:

var link = function(height = 50, color = 'red', url = 'http://azat.co') {
  ...
}

Template literals

Multiline strings

Destructuring assignment

Enhanced object literals

Arrow functions

Promises

Block-scoped constructs: let and const

Classes

Modules

Using ES6 today with Babel

Other ES6 features