freeCodeCamp/guide/english/javascript/es6/index.md

1.6 KiB

title
ES6

ES6

The 6th edition of ECMAScript is called ES6.

It is also know as ES2015.

The changes add a lot of syntactic sugar that allow developers to create applications in an object oriented style.

ES5 example:

var User = function () {
  function User(name) {
    this._name = name;
  }

  User.prototype.getName = function getName(x) {
    return 'Mr./Mrs. ' + this._name;
  };

  return User;
}();

ES6 example:

class User {
  constructor(name) {
    this._name = name
  }

  getName() {
    return `Mr./Mrs. ${this._name}`
  }
}

A lot of new syntax features were introduced including:

  • classes,
  • modules,
  • templating,
  • for/of loops,
  • generator expressions,
  • arrow functions,
  • collections,
  • promises.

Nowadays most of the features are available in all popular browsers. The compatibility table contains all information about feature availability of all modern browsers.

Frequently new features arrive that are part of the successor ES7. A common way is to translate modern JavaScript (ES6, ES7 and other experimental proposals) to ES5. This makes sure that also old browsers can execute the code. There are tools like Babel that transforms new JavaScript to ES5.

Besides syntactic sugar coming from ECMAScript standards there are features that require a Polyfill. Usually they are necessary because whole class/method implementations were added to the standard.