ES6 ECMAScript 2015

Tsai Po-An
Aug 31, 2018 · 2 min read

ECMAScript is the official name of JavaScript language.

ES6 also known as ECMAScript 2015 (the 2015 is named by the year start with ES6).

Below is going to show you some example usage that is new in ES6 compare from ES5.


1. let (Block-Scoped Variables)

let name = 'Dan';
// Here name is Dan
{
let name = 'Max';
// Here name is max
}
// Here name is Dan

2. const (Cannot be changed)

const name = 'Dan';
// Here name is Dan
{
const name = 'Max';
// Here nameis max
}
// Here name is Dan

3. Default Parameter Values

function getName(name = 'Allen') {
// name is Allen if not passed or undefined
return name;
}
getName(); // will return Allen
getName('Charlie'); // will return Charlie

4. Arrow Functions

// ES6
const name = (name) => name;

same as

// ES5
var name = function(name) {
return name;
}

5. Object Property Shorthand

// ES6
let name = 'Me';
const obj = {
name,
getName() {},
[ "Sam" + random() ]: 101
};

same as

// ES5
var name = 'Me';
var obj = {
name: name,
getName: function () {}
};
obj[ "Sam" + random() ] = 101;

6. String Interpolation

// ES6
let name = 'Long';
let say = `I am ${name}!`; // say = "I am Long!"

same as

// ES5
var name = 'Long';
var say = 'I am ' + name + '!'; // say = "I am Long!"

7. For..Of

let arr = [1, 2, 3];for (let n of arr) {
console.log(n);
}
// result
// 1
// 2
// 3

8. Rest Parameter

function test(...arr) {
// arr is an Array
return arr;
}
test('girl', 'boy', 'none') // ['girl', 'boy', 'none']

9. Spread Operator

var numbers = [3, 4, 5];
var
other = [1, 2, ...numbers]; // [1, 2, 3, 4, 5]
var str = "sleep";
var chars = [...str] // ["s", "l", "e", "e", "p"]

10. Class

class MyName {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
},
// Method
fullName() {
return this.firstName + ' ' + this.lastName
}
}
const myClass = new MyName('Long', 'High');myClass.fullName() // Long High

TWJOIN

哲煜科技的小夥伴們的文章

Tsai Po-An

Written by

TWJOIN

TWJOIN

哲煜科技的小夥伴們的文章

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade