An overview of ES6 (EcmaScript2015) the newest version of Javascript. Presentation hold at Bärner JS Talks June 6th 2016
History, new syntax and functions, transpilers, resources
for Netscape Navigator JScript => August 1996 for Microsoft Internet Explorer ECMAScript 1 => June 1997 ES2 => June 1998 ES3 => December 1999 ES4 => July 2008 abandoned! ES5 => December 2009 (ECMA = European Computer Manufacturers Association)
var var a = 5; let b = 5; if(a == b){ var a = 10; let b = 10; // NEW variable console.log('var a:', a); // var a: 10 console.log('let b:', b); // let b: 10 } console.log('var a:', a); // var a: 10, because var is function scoped console.log('let b:', b); // let b: 5, because let is block scoped (new scope in if statement) }
but for let a Temporal Dead Zone (TDZ) is defined until declaration of b console.log(b); // ReferenceError b = 5; // ReferenceError console.log(b); // ReferenceError let b = 10; console.log(b); // 10 let => hosting and Temporal Dead Zone (TDZ) // a is hoisted to the top of the scope console.log(a); // undefined a = 5; console.log(a); // 5 var a = 10; console.log(a); // 10 var a; let b; { TDZ
with or operator function logMyName(firstname, lastname){ firstname = firstname || 'John'; lastname = lastname || 'Doe'; console.log('My name is '+firstname+' '+lastname+'!'); } // ES6: default arguments function logMyName(firstname = 'John',lastname = 'Doe'){ console.log(`My name is ${firstname} ${lastname}!`); }
topic: 'ES6 Overview' }; // ES5: concatenating as usual var greeting = 'Hi! I\'m '+person.firstname+' '+person.lastname+' and I am talking about '+person.topic+'!'; // ES6: using the template syntax with backticks ` var greeting = `Hi! I'm ${person.firstname} ${person.lastname} and I am talking about ${person.topic}!`;
'<div>\n'+ '\t<ul>\n'+ '\t\t<li>a list element</li>\n'+ '\t</ul>\n'+ '</div>'; // ES6: with new backticks ` syntax var someHtmlTemplate = `<div> <ul> <li>a list element</li> </ul> </div>`;
the squares the ES5 way var squares = arr.map(function (x) { return x * x }); // ES6: less verbose code for one-liners Var squares = arr.map(x => x * x); function (x) { return x * x } (x) => { return x * x } x => { return x * x } x => x * x
click handler called'); }; // ES5: the “this” to that assign hack var that = this; button.addEventListener('click', function () { that.handleClick(); }); // ES6: “this” can be used as expected button.addEventListener('click', () => { this.handleClick(); });
dateArray = '2012-01-14'.split('-'); var year = dateArray[0]; var month = dateArray[1]; var day = dateArray[2]; // ES6: destructuring, using a array like syntax on the left hand side of the assignment var [year,month,day] = '2016-04-21'.split('-');
'Käser', topic: 'ES6 Overview' }; // ES5: assign each attribute after the other var theName = person.firstname; var theTopic = person.topic; // ES6: destructuring, using a object like syntax on the left hand side of the assignment var { firstname : theName, topic : theTopic} = person;
first param as delimiter logWords(' and ','apples','bananas','pears'); // should output: apples and bananas and pears // ES5: working with the arguments parameter function logWords(){ var delimiter = arguments[0]; var words = Array.prototype.slice.call(arguments).splice(1); console.log(words.join(delimiter)); }; // ES6: the rest parameter (...) covers the remaining parameters in an array function logWords(delimiter, ...words){ console.log(words.join(delimiter)); };
with our dateString var dateString = '1969-07-20'; function printDate(year, month, day){ console.log(`The date is ${day}.${month}.${year}!`); } var dateArray = dateString.split('-'); // ES5: hardcoding it, or use the apply function printDate(dateArray[0], dateArray[1], dateArray[2]); printDate.apply(null, dateArray); // ES6: the spread operator converts the array to parameters printDate(...dateArray);
array. var arr = ['a','b','c','d']; // ES5: working with the element’s index for (var i = 0; i<arr.length; i++) { var elem = arr[i]; console.log(elem); } // ES6: working with the element’s value for (var elem of arr) { console.log(elem); }
ES6 Learning resources https://github.com/ericdouglas/ES6-Learning ECMAScript on github https://github.com/tc39/ecma262 The TC39 process http://www.2ality.com/2015/11/tc39-process.html