The document summarizes lesser known features of ECMAScript 6 including block scoped variables using let and const, template literals, computed property names, shorthand functions, rest and spread parameters, for-of loops, destructuring arrays and objects, and mixing these features together in examples. It provides code samples to demonstrate each new feature and links to further reading on ECMAScript 6 proposals and specifications.
2. ECMAScript 6 is Coming
Here’s what I’m not talking about:
• Classes
• Modules
• Promises
• Generators
3. Block Scoped Variables
let your variables be more local:
if (true) {
let foo = 1;
const bar = 2;
console.log(foo, bar);
bar = 3; // Throws an exception
}
console.log(foo);
Prints:
1 2
Error: bar is read-only
Error: foo is undefined
7. ...rest Parameters
No More arguments:
myFunc(1, 2, 3, 4, 5);
function myFunc(a, b, ...rest) {
console.log(a, b);
console.log(Array.isArray(rest));
console.log(rest);
}
Prints:
1, 2
true
[3, 4, 5]
8. ...spread Parameters
The opposite of …rest:
let myArray = [2, 3, 4];
myFunc(1, ...myArray);
function myFunc(a, b, c, d) {
console.log(a, b, c, d);
}
Prints:
1, 2, 3, 4
9. for...of Statements
Better* iteration over arrays:
let myArray = ['a', 'b', 'c'];
for (let myElement of myArray) {
console.log(myElement);
}
*not always better
Prints:
a
b
c
Read more at:http://wiki.ecmascript.org/doku.php?id=harmony:proposals
http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts
To play with ECMAScript 6, try Traceur: https://github.com/google/traceur-compiler/wiki/GettingStarted
All code snippets from this talk are available in a packaged Traceur web page at:
https://gitlab.theoreticalideations.com/snippets/2