Using ECMAScript 6
In Chrome, most of the ES6 features are hidden behind a flag called "Experimental JavaScript features". Visit chrome://flags/#enable-javascript-harmony
, enable this flag, restart Chrome and you will get many new features.
Arrow functions are not yet implemented in V8/Chrome, so this flag won't "unlock" arrow functions.
Since arrow functions are a syntax change, it is not possible to support this syntax without changing the way how JavaScript is parsed. If you love developing in ES6, then you could write ES6 code and use an ES6-to-ES5 compiler to generate JavaScript code that is compatible with all existing (modern) browsers.
For example, see https://github.com/google/traceur-compiler. As of writing, it supports all of the new syntax features of ES6. Together with the flag mentioned at the top of this answer, you will get very close to the desired result.
If you want to run ES6 syntax directly from the console, then you could try to overwrite the JavaScript evaluator of the console (such that Traceur preprocesor is run before executing the code). If you fancy doing this, have a look at this answer to learn how to modify the behavior of the developer tools.
Babel is a great transpiler for trying out ES6. You can run ES6 in the browser in the "Try it out" section of their website. It functions similarly to jsfiddle.
Arrows for example:
let add = (x,y) => x + y;
let result = add(1,1);
console.log(result);
displays the result 2
.
Babel "transpiles", that is translate ES6 into ES5 javascript that can be run by current browser technology. You can install Babel via npm install -g babel
. Once installed, you can save the arrows example above into a file. Say we call the file "ES6.js". Assuming you have node installed then at the command line pipe the output to node:
babel ES6.js | node
And you will see the output 2
. You can save the translated file permanently with the command:
babel ES6.js --out-file output.js
output.js "transpiled":
"use strict";
var add = function (x, y) {
return x + y;
};
var result = add(1, 2);
console.log(result);
Which of course can be run in any modern browser.
Example using classes
ES6 is a fast moving target. Refer to the Compatibility Table to find features supported by transpilers such as Traceur and Babel and browser support. You can even expand the chart to see the test used to verify compatibility:
To try out bleeding edge ES6 in a browser try the Firefox nightly build or Chrome release channels