Home > Back-end >  Which version of ECMAScript does the Google Apps Script V8 Runtime Support?
Which version of ECMAScript does the Google Apps Script V8 Runtime Support?

Time:02-13

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?

CodePudding user response:

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.)

CodePudding user response:

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 that it considers syntax errors.

  • Related