22 Nov 2011

ECMAScript 6 looks promising

I am quite excited about ECMAScript 6, after watching David Herman’s talk at YUIConf 2011. I am especially looking forward to seeing some of these features landing up on V8 soon, so that I can use it on node.js. These additions will solve many common sources of frustration that newcomers face when working with JavaScript. Although the spec is not expected to be finalized till 2013 (so says David in the video), a lot of these features are expected to hit Chrome and Firefox much before that.

I am personally looking forward to the following:

let keyword

The let keyword has a block scope. Since var has a function scope, it sometimes leads to unintended bugs. Going forward, we can completely replace usages of var with let to avoid such bugs.

Default arguments

function foo(bar="baz") {
   console.log(bar);
}

With default arguments, we don’t need the convoluted options object pattern.

Non-strict destructuring

Looks Pythonic, except that it’s not as strict.

let [x,y] = [3,4,5];   // x=3, y = 4

Multi-line strings
You can simply use the ` (backtick) operator to denote multi-line strings.

var htmlString = `Say hello to 
multi-line
strings!`

Templating
You can also embed JavaScript variables amidst your strings this way:

var firstName = "Jack";
var message = `Hello ${firstName}!`; // "Hello Jack!"

List comprehension

Again, a very pythonic construct:

let even = [ x for (x in values([1,2,3,4,5,6])) if (x %2 === 0) ];

The use of values() allows x to denote the element values, rather than the element indices. This could also be written using the new for of construct:

let even = [ x for(x of [1,2,3,4,5,6]) if (x%2 === 0) ];

Apart from these, map, filter, reduce and so on will be part of the gang (some of these are already implemented in Chrome, Firefox, and (gasp) IE9).

UPDATE:

Noticing that this post has hit the frontpage on HN, I must add that I left out the proposed module system. Examples from the talk:

import { $ } from "jquery.js"
import { map, each } from "underscore.js"

Once again, it’s all Python! The referred JavaScript files will actually be loaded by your browser before the code is executed.

You can follow me on Twitter right here.

Tagged with:

29 people have responded to this post.

Leave a Reply