Submitted to ECMA in 1996
Edition
Publish Date
Editors
1
2
3
4
5
5.1
6
7
June 1997
June 1998
December 1999
Abandoned
December 2009
June 2011
June 2015
TBD
Pratap Lakshman, Allen Wirfs-Brock
Pratap Lakshman, Allen Wirfs-Brock
Allen Wirfs-Brock
function multiply(a, b) {
b = typeof b !== 'undefined' ? b : 1;
return a * b;
}
function multiply(a, b = 1) {
return a * b;
}
var reflect = function(value) {
return value;
};
// same as
var reflect = value => value;
Benefits:
var a = [
"Hydrogen",
"Helium",
"Lithium",
"Beryllium"
];
var a2 = a.map(function(s){ return s.length });
var a3 = a.map( s => s.length );
Benefits:
function Person() {
var self = this; // Some choose `that` instead of `self`.
self.age = 0;
setInterval(function growUp() {
self.age++;
}, 1000);
}
Benefits:
function Person(){
this.age = 0;
setInterval(() => {
this.age++;
}, 1000);
}
var p = new Person();
Benefits:
"This string is so long that
it wouldn't fit on one line"
// SyntaxError
`There is no poetry
without line breaks`
// Art
Benefits:
var difficult = "fun";
var boring = "exciting!";
`Learning es6 is ${difficult} and ${boring}?`;
var a = "foo",
b = 42,
c = {};
var o = {
a: a,
b: b,
c: c
};
var a = "foo",
b = 42,
c = {};
// Shorthand property names (ES6)
var o = { a, b, c };
var obj = {
myMethod: function () {
···
}
};
let obj = {
myMethod () {
···
}
};
var property = 'foo';
var object = {};
object[property] = true;
var property = 'foo';
var object = {
[property]: true
};
var foo = "bar"
console.log(foo) // "bar"
let baz = "qux"
console.log(baz) // "qux"
// If they are in the same scope they can act similarly
for (var i = 0; i <= 2; i += 1) {
i;
}
console.log(i); // 3 (I was not supposed to stick around)
for (let i = 0; i <= 2; i += 1) {
i;
}
console.log(i); // ReferenceError (Good. i is gone)
function kickRocks() {
console.log(rock_type); // undefined
var rock_type = "granite";
console.log(shoe_type); // ReferenceError
let shoe_type = "Nike";
}
const color = 'red';
var color = 'blue'; // can't do it
color = blue; // nope
let color = blue; // still no
console.log(color + 'is my favorite color') // okay
function Account(username, domain) {
this.username = username;
this.domain = domain;
}
var bad_account = new Account(..."bogus.user@notmail.com".split('@'));
console.log(bad_account);
// Object { username: "bogus.user", domain: "notmail.com" }
function validatePresenceOf(object, ...properties) {
properties.forEach(function (property) {
if (!object.property) {
alert(`${property} cannot be blank for ${object.name}!`);
}
});
}
var contact_info = {
name: "Cat Stevens",
phone: "409-528-1766",
email: ""
};
validatePresenceOf(contact_info, 'phone', 'email');
// alert: email can't be blank for Cat Stevens!
var fishes = {
one : 'fish', two : 'fish',
red : 'fish', blue : 'fish'
};
for (fish of fishes) { console.log(fish); }
// logs ['one','two','red','blue']
for (fish in fishes) { console.log(fish); }
// logs ['fish','fish','fish','fish']
var people = [
{ name: 'Fred', age: 40 },
{ name: 'Barney', age: 36 },
{ name: 'Pebbles', age: 1 }
];
people.find( function (person) {
return person.age < 40;
});
// { name: 'Barney', age: 36 }
people.find( function (person) {
return person.age > 40;
});
// undefined
var people = [
{ name: 'Fred', age: 40 },
{ name: 'Barney', age: 36 },
{ name: 'Pebbles', age: 1 }
];
people.findIndex( function (person) {
return person.age < 40;
});
// 1
people.findIndex( function (person) {
return person.age > 40;
});
// -1
var quote = 'To be, or not to be';
quote.includes('bee'); // false
quote.includes('be'); // true
quote.includes('be', 1); // false
quote.includes('to'); // false
quote.includes('To'); // true