3

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?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Senseful
  • 86,719
  • 67
  • 308
  • 465
  • 2
    Possible duplicate [V8 Runtime for Google Apps Scripts](https://stackoverflow.com/q/60170588/1595451) – Rubén Feb 12 '22 at 07:37
  • 2
    Engines don't support a specific version of EcmaScript, it's not like specs publish a new version then engines implement all of it in a new version of the engine, it's engines build and add new features, specs add new features, engines find bug in specs, specs find some features from engine worth moving to the specs and all this at the same time. So your question should not be what version of ES is supported, but if X or Y feature is available. – Kaiido Feb 12 '22 at 08:46
  • 2
    "*I could easily assume that "modern" refers to the latest, 12th edition (2022) of ECMAScript*" - I would refer to that one as a future edition, not a "modern" one, as it hasn't been released yet :-) – Bergi Feb 12 '22 at 11:40
  • 2
    I guess you should not ask "*Which version of ECMAScript is supported by the V8 runtime?*" but rather "*Which version of V8 does Google Apps Script use?*". The supported features of the various V8 versions are [well-documented](https://v8.dev). – Bergi Feb 12 '22 at 11:43

2 Answers2

4

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

jmrk
  • 34,271
  • 7
  • 59
  • 74
  • 1
    According to [comment #6](https://issuetracker.google.com/u/0/issues/194120497#comment6) from a googler posted on July 30, 2021, Apps Script V8 runtime supports EMCA2015. H/T Andrew James (see their [answer](https://stackoverflow.com/a/69208380/1595451)) – Rubén Feb 13 '22 at 18:47
  • 2
    According to [comment #2](https://issuetracker.google.com/issues/195752915#comment2) from a googler posted on Aug 9, 2021 some V8 / JavaScript features that were blocked by the parser were filled internally as bugs. H/T TheMaster (see their [answer](https://stackoverflow.com/a/71045342/1595451)) – Rubén Feb 13 '22 at 18:55
1

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.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • 1
    An interesting observation; although personally I'd rather skip numeric separator syntax for now than litter my code with `eval` :-) – jmrk Feb 12 '22 at 20:52