When you crate a new Google Apps Script, it seems to support the v8 runtime by default. The documentation states:
Apps Script supports two JavaScript runtimes: the modern V8 runtime and an older one powered by Mozilla's Rhino JavaScript interpreter.
The V8 runtime supports modern ECMAScript syntax and features.
The V8 runtime documentation states:
You can use modern ECMAScript syntax in scripts that are powered by the V8 runtime. This syntax includes let, const, and many other popular features.
In both cases, they are very vague as to which ECMAScript version is supported, simply stating "modern ECMAScript syntax". This is problematic because there are 7 versions that were released between 2015 and 2021. Thus "modern" could refer to any one of these versions.
For example, I could easily assume that "modern" refers to the latest, 12th edition (2022) of ECMAScript, and end up writing code like this:
let a = 1_000;
However, attempting to use that syntax leads to the error:
Syntax error: ParseError: Unexpected token ILLEGAL line: ...
Rather than manually go through each of the remaining 6 versions until I find the latest one supported, it would be great to find documentation that explicitly states which ECMAScript version is supported.
Note: The previous related question (Which Edition of ECMA-262 Does Google Apps Script Support?) is not helpful since those answers also refer to "modern ECMAScript" rather than a definitive, specific version.
Which version of ECMAScript is supported by the V8 runtime?
There is some nuance here:
Which version of V8 does Google Apps Script use?
A reasonably recent version, and it gets updated every so often. I believe the idea is to track or slightly lag behind stable Chrome releases, but (as with any large project updating its dependencies) there may occasionally be hiccups/delays. Right now it should be somewhere in the 9.x version range. (For future readers: I expect this statement to be outdated before 2022 is over!)
Which version of ECMAScript does the Google Apps Script V8 Runtime Support?
I suppose if there was a simple answer to this, you'd find that in the documentation. As #Kaiido said in comments, JavaScript engines implement new JavaScript features one by one (rather than EcmaScript versions). So, for browsers just like for environments like GAS, it usually makes more sense to ask "is feature X supported?", because it may well be that some, say, ES2020 features are still missing but some ES2021 features are already available.
Why does let a = 1_000; produce a Syntax Error?
Well, the V8 version that GAS uses is sufficiently new (by at least two years) to support it; but the overall GAS experience depends on more than V8: the editor is parsing the entered source in order to provide help or highlighting or error checking or whatnot. It looks like the GAS team is aware that certain features aren't supported yet by the components responsible for that, and is actively working to remedy that. (I have no idea what the timeline is.)
Why does let a = 1_000; produce a Syntax Error?
Just to expand on #jmrk's answer about new features not supported by the parser.
function test2564(){
//let a = 1_000; throws syntax error by the parser
console.info(eval(`1_000`));// correctly logs 1000
}
The underlying V8 engine is good and supports the latest features, but the parser won't allow you to save or execute the project with those features, as it considers them as syntax errors.
Related
I have been reading through a book called You don't know JS and one thing that I can't wrap my head around is the concept of backward compatible and forward compatible Javascript.
From what I understand:
Backward Compatible: Once something is added to Javascript specification, it would never become invalid JS in future
Forward Compatible: Including a new addition to the language in a program would not cause that program to break if it were run in an older JS engine
Javascript is backward compatible and not forward compatible. This means that while including new feature in a program might break the program (forward compatible), Javascript engines would support older syntax forever in future (backward compatible).
To solve a problem of forward compatible issue, developers use transpilation and polyfills to convert new syntax to an older syntax supported in older Javascript engine.
So if transpilation is used to convert code into forward compatible code, does that mean Babel is used to solve forward compatibility issue and not backward compatibility issue?
This comes from the official doc in Babel website:
Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments.
I must have missed something and need some clarification on how to think through this.
Edit 1
I thought it would be helpful to quote parts of the book here:
Typically, forwards-compatibility problems related to syntax are solved by using a transpiler (the most common one being Babel (https://babeljs.io)) to convert from that newer JS syntax version to an equivalent older syntax.
Developers should focus on writing the clean, new syntax forms, and let the tools take care of producing a forwards-compatible version of that code that is suitable to deploy and run on the oldest-supported JS engine environments.
I believe your confusion is rooted in the mixing of the forwards- and backwards-compatibility terms for both the operating environment or language, and individual scripts themselves.
In the You Don't Know JS doc, the writer is describing the implementation of the language as whole as being backwards compatible, in the same way that many video games released for early generations of consoles (such as PlayStation and XBox) are able to played on their more recent successors (PlayStation 2 and XBox 360) due to the console (and its architecture, operating system, etc) being backwards compatible. When applied to JS, this means that a browser implementing a newer version such as ECMAScript 2015+ will fully support code written using an older version.
Babel, on the other hand, is referring to the code itself, and being able to create a backwards compatible version which can be run on older browsers, for example. To expand on the example above, this would be akin to being able to process an XBox 360 game through some engine (i.e. Babel) and have it give you a fully functional game to play on your original XBox. This is clearly a much more difficult problem, but is required due to JS not being forward-compatible.
As a long time programmer just getting into JavaScript programming, I have the following questions that are still unclear despite having read many articles.
Looking at the ES6 (ECMAScript 2015) support by browsers, I can see that the supporting level is much less than that of Node.js, so the question is,
If both Node.js and browsers are using the modern V8 engine, why supporting level are so different?
Looking at the ES6 support in Node.js, I can see really really few ES6 features are unsupported now. However, what exactly does the supported means in the chart? I.e.,
Does it means even I write using the support ES6 features, I still need to use the Babel compiler to compile ES6 code to ES5 for Node.js to use it?
For TypeScript ES6-style JavaScript code that runs for Node.js, they are still need to be transpiled into an ES5 compatible form, despite that Node.js almost cover all ES6 featues, right? I.e.,
for the following code,
class Animal {
constructor(public name) { }
move(meters) {
console.log(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
move() {
console.log("Slithering...");
super.move(5);
}
}
class Horse extends Animal {
move() {
console.log("Galloping...");
super.move(45);
}
var sam = new Snake("Sammy the Python")
var tom: Animal = new Horse("Tommy the Palomino")
sam.move()
tom.move(34)
Does it need to be transpiled into an ES5 compatible form to runs with Node.js or not?
Finally, any online site that I can try playing with TypeScript/ES6 code like above?
I copy it to my chrome console, and got an error that I don't understand - Unexpected strict mode reserved word, and
I tried it on http://www.typescriptlang.org/play/index.html, but the console output is not working there.
Please help. thx.
Looking at the ES6 (ECMAScript 2015) support by browsers, I can see that the supporting level is much less than that of Node.js, so the question is,
Many different browsers and many different Javascript engines in them, each with their own level of ES6 support. The latest version of node.js is generally pretty up-to-date on what the V8 engine supports. Many browsers have longer release cycles and may not be as current, but each is different and has their own release strategy and level of ES6 support.
If both Node.js and browsers are using the modern V8 engine, why supporting level are so different?
If you compare the latest release of node.js with the latest release of Chrome on Windows, you won't see much difference in support. The ES6 support chart you're looking at seems old to me. For example, Chrome has had support for the Set object for a long time, but your chart says false.
Looking at the ES6 support in Node.js, I can see really really few ES6 features are unsupported now. However, what exactly does the supported means in the chart? I.e.,
Supported means you can use the feature directly without a transpiler, but how accurate that is depends upon the source of the document claiming it. Some documents do extensive testing of all the various edge cases of a given feature.
Others just look for general implementation. So if for example, you're looking at support for the Set object and it says "supported", then that is suppose to mean that you can just write plain Javascript that uses the Set object and it will just work. How accurate that document is depends upon the source of their data and the thoroughness of their testing.
Does it means even I write using the support ES6 features, I still need to use the Babel compiler to compile ES6 code to ES5 for Node.js to use it?
No. In a Javascript engine that supports a given feature in ES6, you can write ES6 code for that feature and directly run it in that Javascript engine. No transpiling is needed.
For TypeScript ES6-style JavaScript code that runs for Node.js, they are still need to be transpiled into an ES5 compatible form, despite that Node.js almost cover all ES6 featues, right? I.e.,
The class definitions you show are plain ES6 code. Those will work just fine as is in an ES6 capable Javascript engine.
If you write Typescript code, then you will have to transpile the TypeScript to Javascript because no Javascript engine (I know of) supports TypeScript directly. When transpiling form TypeScript to Javascript, you can usually specify whether you want the transpiler to generate ES5 compatible code (which will run in an ES5 engine or an ES6 engine) or ES6 compatible code (which will only run in an ES6 engine) depending upon what your target environment is capable of.
Does it need to be transpiled into an ES5 compatible form to runs with Node.js or not?
Your particular code appears to contain at least one TypeScript style variable declaration which would need to be transpiled. The rest looks like plain ES6 Javascript which should work in any ES6 engine without transpiling.
When I remove the TypeScript, fix some syntax errors in your code and implement the Animal constructor properly, then this code works fine in node.js v8.8.1 (which is what I currently have installed) and in Chrome 63.0.3239.132, Edge 41.16299.15.0 and Firefox 57.0.4 all on Windows 10:
// Generic ES6 code
class Animal {
constructor(name) {
this.name = name;
}
move(meters) {
console.log(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
move() {
console.log("Slithering...");
super.move(5);
}
}
class Horse extends Animal {
move() {
console.log("Galloping...");
super.move(45);
}
}
var sam = new Snake("Sammy the Python");
var tom = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);
You can run this snippet yourself in any browser you desired to see the results (assuming the browser is modern enough to support a stack overflow snipppet). It works in all the current versions of browsers I have except IE 11.192.16299.0 (no surprise that IE doesn't support ES6).
I copy it to my chrome console, and got an error that I don't understand - Unexpected strict mode reserved word,
This happened to me when I tried to run your code in node.js until I removed the TypeScript from it so that it was just plain ES6. I think this particular error is caused by the public in this line:
constructor(public name) { }
since that is not part of the ES6 specification (it's apparently part of TypeScript).
It seems that there's one question you're dying to ask, but haven't exactly articulated is: "How do you know whether you have to transpile or not?".
The answer is that you have to understand the cross between the target environments you wish to run in and the newest features you plan to use. If you are writing server-side code that will only run in node.js, then it's a lot simpler. Examine a comprehensive table such as http://node.green/, study what it says for the node.js version you plan to use and the feature in question. If it indicates you should be able to use that feature, then write your code using that feature, write a test case for it and verify that both the code you wrote and the feature you are using both work. Add that to your body of knowledge about what you can and can't use in that version of node.js. You can then assume all future versions of node.js will also support that feature.
If you're writing code to run in a browser, life is much more complicated. If you plan to support a lot of browsers and really don't want to worry about ES6 support at all, then just transpile to an ES5 target and go about your business.
If you want to use non-transpiled code, then you have a lot of testing to do in a lot of browsers. You have to first specify exactly which versions of which browsers you are going to support and then you have to write your code and test cases and you have to test in every browser you plan to support. There really is no shortcut. When you find things that don't work, you'll have to either look for polyfills or work-arounds or stop using that ES6 feature.
Test the code in the environments that the code is going to be used in. Use the available means to implement the specific standard or specification within the environment that you are using the code at. Or try to create an approach yourself to resolve an issue that you encounter during development of your code while noting the progressions and persistent issue for others to be able to possibly address and resolve the issue, bug or requirement from their own perspective.
Simply due to the fact the a document states that the browser has implemented a specification or standard does not mean that the implementation is consistent with the specification, or implemented at all. The only way to verify whether a browser implements a standard is to test with code yourself. File issues and attempt to fix bugs yourself.
Browsers use different engines including Gecko, WebKit, not V8 alone; and can change over time in both name and implementations of specifications; see Monitor and potentially deprecate support for multitrack SourceBuffer support of 'sequence' AppendMode; How to use "segments" mode at SourceBuffer of MediaSource to render same result at Chomium, Chorme and Firefox?. There are many browsers. For example, Lynx does not use V8.
See web platform tests
The web-platform-tests Project is a W3C-coordinated attempt to build a
cross-browser testsuite for the Web-platform stack. Writing tests in a
way that allows them to be run in all browsers gives browser projects
confidence that they are shipping software that is compatible with
other implementations, and that later implementations will be
compatible with their implementations. This in turn gives Web
authors/developers confidence that they can actually rely on the Web
platform to deliver on the promise of working across browsers and
devices without needing extra layers of abstraction to paper over the
gaps left by specification editors and implementors.
For example, one test for Web Speech API, where volume property is specified as capable of being set, though was not able to detect a change of audio output for either Chromium or Firefox when setting the volume property of SpeechSynthesisUtterance to different values within the specified ranges.
Specifications are a totally different regime than actual browser implementations. Specifications or standards can be and are written well in advance of actual browser implementation, if implemented at all. You can use browserify, or write the code yourself to use NodeJS modules or other non-native code in the browser.
I've been working with KoaJS for a while, and we can easily use the 'let' keyword and the generators when using the --harmony flag but I couldn't find how much support for does the node v0.11.x provides while using the same.
I tried using the default value argument initialization but couldn't succeed.
Is there any source available which can list the no of features of ECS 6 supported in node v0.11.x using the harmony flag? Or if there is any npm module available for node that might allow me to use the same?
Thanks in advance.
With regards to your second question, yes, there is es6-module-loader.
For a long list of transpilers, shims, and other tools for using full ES6 features now, see addyosmani's ECMAScript 6 Tools page.
As for native ES6 support in node.js, V8 officially implements "ECMAScript" but AFAIK the V8 project doesn't release a spec of their implementation.
However there are some sources of useful information out there.
Here's a brief overview of ES6 in node.js v0.11.6.
You may want to determine the version of V8 that your version of node.js uses.
See the node.js blog for recent changelog info.
It can also be useful to find the version of V8 used in a given Chromium release.
The Chrome release notes can be found here.
Keep in mind that different flags can be set for the same version of V8.
Chromium and node.js both have ways to set flags in V8 related to ES6 support.
Here are two tables that list ES(6) feature support across implementations:
http://pointedears.de/scripts/test/es-matrix/
http://kangax.github.io/compat-table/es6/
This MDN page lists a set of reference articles for ES6 language features.
At the bottom of each one you can see the status of Chrome support for that feature (and using V8 versions determine the support in node.js).
Finally, the V8 issue tracker
provides a list of issues related to ES6 features, many of which have been implemented and their issues closed.
You can use ~96% of ES6 features in Node.js 6. You can review support for all versions on http://node.green/
This does not concern node 0.11, but in the current 5.8.0, you can use --harmony_default_parameter.
It it scheduled to be included by default in v6.0.
Node.JS v0.11.3 claims to have support for ECMAScript 6 modules with the flag --harmony_modules.
I have tried various examples, such as the following.
module math {
export var pi = 3.141593;
}
What is the syntax to get modules working in Node.JS?
The modules implementation in V8 is incomplete. There's parsing support when enabled with --harmony-modules, but support of the actual functionality was put on hold. The reason for this is because the specification for how ES6 modules will actually work has been in the works and is still not fully nailed down.
The implementation in Continuum (the linked screenshot from Crazy Train's answer) dates back to an interim spec from November 2012 and is now woefully out of date because of the ongoing changes to the ES6 module's spec. This is why the V8 devs put development of support for modules on hold.
It seems like the modules spec is approaching stability (though I expect we'll see small refinements for a while) and I think (hope at least) that we'll see SpiderMonkey and V8 moving forward with implementations over the next 6 months.
Useful links:
V8 modules bug: https://code.google.com/p/v8/issues/detail?id=1569
SpiderMonkey modules bug: https://bugzilla.mozilla.org/show_bug.cgi?id=harmony%3Amodules
You can use Continuum, which is an ES6 virtual machine written in (current) JavaScript.
I just tried in Firebug console,
let (X=10) X/2
and
[x,y]=[y,x]
These are features supported by SpiderMonkey, I guess V8 has its own share.
Where can I learn of features that are not yet included in ECMAScript, but work in various browsers? Is there a place where these are collected together?
ECMAScript 6 (a.k.a. ECMAScript 2015) is the current standard for JavaScript, but engines have yet to implement it completely:
Table showing what ES6 features are supported where: http://kangax.github.io/compat-table/es6/
Three books on ES6 that are free to read online (buy them I you like them!):
Read Understanding ECMAScript 6
Exploring ES6
JavaScript Allongé, the "Six" Edition
ES6 in bullet lists
Starting with ECMAScript 2016, there will be yearly releases and a new release process:
Blog post explaining the new release process and the features that are candidates for ES2016: http://www.2ality.com/2015/11/tc39-process.html
Official list of proposed features (that may or may not be accepted for the ECMAScript standard): https://github.com/tc39/ecma262
Feature table for ES2016: http://kangax.github.io/compat-table/es7/
If you want to use any of the new features even on older engines, you can transpile them to ES5 via Babel: https://babeljs.io/
And here is a article covering various resources around Harmony/ES6/Javascript.next:
http://addyosmani.com/blog/ecmascript-6-resources-for-the-curious-javascripter/
That first feature is known as a "let expression" and it is nonstandard; it was dropped from Firefox 41, and the similarly nonstandard "let block" was dropped from Firefox 44: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Non-standard_let_extensions
I was surprised to find that this particular non-standard JS was not mentioned in Kangax's table, but I guess he had to restrict this list to non-standard JS extensions that are supported by multiple engines: https://kangax.github.io/compat-table/non-standard/
If you want to go deeper down the rabbit-hole, and Kangax and MDN haven't satisfied your curiosity, this old reference may tell you about curiosities in older browsers: help.dottoro.com/ljsdaoxj.php
Beyond that, the browser-makers usually document the quirks of their own browsers (MDN is also good about documenting non-Mozilla quirks, but it's not perfect); speaking of quirks, Peter-Paul Koch documents both standard and non-standard DOM methods here: quirksmode.org/dom/
Anyway, these aren't just "not yet" in the standards, but likely "not ever" and you shouldn't use them in your own code.
For current ECMAScript 264 implementation here is a list of features supported by different browser vendors:
http://kangax.github.com/es5-compat-table/
For the next generation ECMAScript Harmony some resources:
http://addyosmani.com/blog/ecmascript-6-resources-for-the-curious-javascripter/
http://kangax.github.com/es5-compat-table/es6/