The next version of javascript should be approved around December this year. While we’ve been able to play with parts of ES6 for some time now (e.g. behind a Chrome flag, behind node’s --harmony flag, etc.), integrating them into the language and having wide support will truly make Javascript a better language.

There are so many additions to the language, but this is just a focus on functions. More for my own sake, but some examples of things I’m excited about. Many of them move javascript toward a more functional language.

Splat - ...

The spread operator handles several of the problems with arguments. Also called rest or spread operator. Unlike arguments, these are real arrays so you can use array functions.

Currying

From Douglas Crockford’s JSConf UY talk

function curry(func, ...first) {
  return function(...second) {
    return func(...first, ...second);
  };
}

Proper Tail Calls

When a functions returns a function call as its final action, this is a tail call. This is common in recursion. The outer function will not perform any more work, and ES6 takes advantage of that fact - instead of creating a new stack frame (more memory), the language will instead reuse the parent’s stack frame.

In theory, you could avoid while loops without a performance hit:

function repeat(func) {
  if (func() !== undefined) {
    return repeat(func);
  }
}

Generators

Generator functions (MDN, denoted by function*, create iterator objects. I wrote about them a bit ago, and MDN has good examples.

Arrow Syntax

Beyond the shorter syntax, arrow functions allow you to maintain the value of this of the enclosing context:

function MyObject () {
  this.count = 0;
  setInterval(() => {
    this.count++; //this still refers to the MyObject object
  }, 1000);
}

Some of the syntax can be hairy (and hard to read)… For example, the spec notes you need to wrap a returned object literal in parentheses so it is not confused for the function body:

// Parenthesize the body to return an object literal expression
let key_maker = val => ({key: val});

Default parameters

You can set default values for function parmaters (MDN), which (unlike python) are evaluated at run time, creating a new object each time:

function append(value, array = []) {
  array.push(value);
  return array;
}

append(1); //[1]
append(2); //[2], not [1, 2]

Expression Closure Shorthand

Javascript scoping can be really weird. Closures (See MDN) have been the de facto way of dealing with some of these issues. Now they can be shortened, moving from

function(x) { return x * x; }

to

function(x) x * x

Other ES6 Features

There’s a lot more in this release, and you can find more information in the links below:

ES6 Proposal Wiki