Using ECMAScript 6
Asked Answered
T

5

164

I'm looking for a way to run ECMAScript 6 code in my browser's console but most browsers don't support functionality that I'm looking for. For example Firefox is the only browser that supports arrow functions.

Is there a way (extension, userscript, etc.) I can run these features on Chrome?

Tennietenniel answered 3/6, 2014 at 6:22 Comment(0)
U
158

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.

Unclinch answered 3/6, 2014 at 9:29 Comment(9)
There is also a ScratchJS, a dev tool for Chrome:chrome.google.com/webstore/detail/scratch-js/…Hitandmiss
Arrow functions are now fully implemented in the latest version of chrome. This tip remains useful for other ES6 features though. Like the class syntax, for example.Florid
This answer is now mostly outdated.Sacttler
@Gothdo The specifics are indeed outdated (arrow functions are well-supported these days), but the general advice for enabling experimental JS functions is still true. For instance, look-behinds in regular expression are still disabled by default. The following will return false if the --enable-javascript-harmony flag is set: /(?<!a)b/.test('ab') (and throw the next error if the flag is not set: "Uncaught SyntaxError: Invalid regular expression: /(?<!a)b/: Invalid group")Unclinch
It's not outdated, had the same problem with arrow_functions for a client with an old version of windows. The flag did the trick, thanks!Indigo
Hey, I was wondering if there is a way to check/make sure this flag is enabled for an Electron application. Also, where can I find documentation on the EcmaScript version/Feature Support for a given Chrome Version (54.0)Amygdalin
@Amygdalin I don't know about Electron, but for Chrome you can look at kangax.github.io/compat-table/es6Unclinch
@RobW, This answer needs an update. E.g. Arrow functions are available in Chrome without any flags needed.Impostume
To get as many ES6 features as possible inside Chrome Canary, you will want to enable the Experimental Web Platform flag as well as the Experimental JavaScript flag. Also see medium.com/dev-channel/…Biotope
V
47

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:

enter image description here

To try out bleeding edge ES6 in a browser try the Firefox nightly build or Chrome release channels

Vish answered 7/3, 2015 at 4:5 Comment(2)
If you haven't already heard of it, checkout jspm. It allows you to consume CommonJS, AMD, and ES6 modules. It relies on Traceur or Babel to "compile" the ES6 to ES5 in the browser before execution. One of the nice benefits of jspm is the ability to consume npm modules or (with a bit of configuration) modules from Github. Other module repositories are likely to be added in the future. jspm also has pretty decent browser support. Not tested, but I believe it is IE9+. (Your ES6 compiler affects this too.)Jewelry
In Ubuntu 17.04, I need to do sudo npm install -g --save-dev babel-cli babel-preset-es2015 and $(npm bin)/babel ES6.js --presets es2015 to make it work, rf: babeljs.io/docs/plugins/preset-es2015Grape
P
12

You're probably looking for one of the following links:

Babel (for Webpack, for Gulp, for Grunt, for other frameworks & languages)

Using Babel in your development pipeline will automatically transpile (convert) your JavaScript to be cross-browser compatible. Or, if you're using TypeScript, you can rest easy; your code is already getting transpiled.




Don't want to setup a transpiler (such as Babel/Typescript), or do you want to play with features not yet supported by your transpiler?

You can enable experimental ECMAScript features in your browser by going to chrome://flags/#enable-javascript-harmony and enabling the JavaScript Harmony flag. For some features, you may have to use Chrome Canary with the JavaScript Harmony flag enabled.

JS harmony screenshot

New JavaScript APIs are not usually covered by Babel, and will have their own Chrome flag.


Using Arrow functions

This question specifically mentioned using arrow functions. Arrow functions are now natively supported in all browsers except IE and Opera Mini. See caniuse.

It used to be a little difficult if you wanted to play with arrow functions. The below history shows what it took at different points in time to play with this feature.

1) At first, arrow functions only worked in Chrome Canary with the chrome://flags/#enable-javascript-harmony flag enabled. It looks like this functionality was at least partially implemented by version 39.

2) Then, arrow functions where made available in Google Chrome behind the JavaScript Harmony flag.

3) And finally, in Google Chrome 45, arrow functions where enabled by default.

You can expect a similar pattern to happen with other new ECMAScript features.

Pacifism answered 29/8, 2015 at 14:41 Comment(1)
TypeScript comes with a great transpiler. Babel is a common JavaScript transpiler as well. If you transpile your es6 code down to es5, you won't have to wait for browser support to start using the awesomeness of es6!Pacifism
A
6

Just use the use strict mode, into a closure (not needed, but it's a great approach) and Chrome will be able to execute your code as ES6...

(function(){
  'use strict';
  //your ES6 code...
})();

Here an example... http://jsbin.com/tawubotama/edit?html,js,console,output try to remove the use stric mode line, and try again, an error on the console will be logged.

Aesthetically answered 20/12, 2015 at 21:25 Comment(0)
P
3

As of 2015 Nov, Firefox and Edge have been leading the ES6 races, use them if you want to experiment functions that Chrome lacks. Edge has class/subclass and Firefox has Proxy; between them you have virtually all ES6 features.

If you must use ES6 in Chrome console, there is one simple, tried and true way:

Be patient.

Browsers are adopting ES6 - even Safari, who has been dragging its feet on most HTML5 standards. Give Google time and they will implement ES6 features one by one. For example arrow functions is now available, in production channel and without flag.

Don't expect extensions; you can't translate ES6 to ES5 line by line, so we can't just extend console with Babel.

It is true that there is an experiment js flag, but it exists for good reasons. V8 has Proxy but in old (non-standard) syntax and has security issue. Its destructuring is also incomplete: you will find that in some case it works, in come cases it doesn't.

If you just want to play ES6, simply play with Edge / Firefox. They both cover almost whole Babel, have console and debugger, and have features that Babel cannot provide.

Piper answered 24/11, 2015 at 8:40 Comment(2)
Safari is actually ahead of all desktop browsers and ios10 mobile is ahead of Chrome for Android in ES6 support. kangax.github.io/compat-table/es6Kenon
@Louis Before iOS 10, Safari has neglected emerging web tech. Even now, half a year after it, 21% of old iOS users are stuck with ES5 as older iOS can't upgrade only the browser, and is still the solo force holding back ES6 aboption in mobile dev (98% Androids can run latest Chrome). I'll keep this answer unchanged as both Q and A are obsolete, but if you look just slightly beyond ES6, Safari 10 is still lagging behind by missing out async function or fetch api.Piper

© 2022 - 2024 — McMap. All rights reserved.