-1

I've been stuck trying to debug a script I was writing when it dawned on me that the source of the problem could be that the Array.prototype.find() method I was using is not supported at all in the current version of Google Apps Script (as of March 4th, 2017). The reason I suspect this is because of the following code that I tried running in two different environments:

// ran this locally on Chrome version 56.0.2924.87 (64-bit)
// result is 3
var t = [1,2,3,4];
var x = t.find(function(v) {
  return v == 3;
});
document.write(x);

==========

// ran this on Google Apps Script (whatever the version is as of March 4th, 2017)
// attempting to run this function produces the error: "TypeError: Cannot find function find in object 1,2,3,4."
var t = [1,2,3,4];
var x = t.find(function(v) {
  return v == 3;
});
Logger.log(x);

The only conclusion I could draw from this experiment is that Google Apps Script is based on an outdated version of JavaScript, which leads to my main question: How can I find which version of any technology (whether it be JavaScript, or HTML, or CSS, etc.) is supported by the Chrome web browser and Google Apps Script? How could I best keep myself informed of any major changes?

Manuel
  • 2,143
  • 5
  • 20
  • 22
  • Possible duplicate of [Google Apps Script Javascript Standard Support](https://stackoverflow.com/questions/37768501/google-apps-script-javascript-standard-support) – Rubén Jan 30 '19 at 02:55

2 Answers2

2

This may appear to be a non-answer at first, but that's because you are asking the wrong question. You shouldn't be asking "what version of JavaScript" is supported, but "what features are available in this JavaScript environment," and this you can test for at runtime. (It's possible for an environment to support some parts of ES6 and not others, so those environments are neither purely ES5 nor ES6 -- there isn't a simple "Google Apps uses version ___" answer.)

Feature testing is the canonical way to write portable JavaScript, and since asking for something that isn't there returns undefined1 instead of throwing an error, you can very easily see what's available to you.

For example:

if (Array.prototype.find) {
    // Arrays have find()
} else {
    // Arrays do not... maybe polyfill an implementation?
}

1 Some so-called "native objects" (not pure JavaScript objects, but those provided by the embedding environment) might not behave this way. Famously, some DOM objects in some browsers will throw when you access a property that does not exist. (But why would you directly manipulate the DOM when we have libraries like jQuery?)

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • Thank you, this was very helpful to know. But now I am wondering if there's a way to check which features are supported in any one environment all at once, instead of having to test each and every method one-by-one, or by writing a long chain of if-and-else statements. For example, I tried saving Array.prototype to a variable and using a for-in loop to print its properties but this prints nothing to the screen, and when I check the length of the object (i.e. obj.length), it prints a length of 0. – Manuel Mar 04 '17 at 20:36
1

The best resource I have found (and it is a good one), is this link.

It gives not only browser support, but mobile and transpiler support as well and you can switch between JavaScript versions (even future ones) to get a sense of how browsers are progressing. I don't think it covers Google Apps Script.

This site is packed with features. One of my favorites is clicking on a language feature on the left and seeing the various aspects needed to support the feature. It even provides code samples that verify conformance.

An amazing resource that I reference regularly.

If you want to get a run-time sense of what is happening in a particular environment, then you could consider copying the function tests provided in this link to your own test script and executing that as a way to determine compliance for a particular feature.

For example, suppose we want to test for basic support of an ES6 feature - defaulting function parameters. If we click on 'default function parameters' and then click on the little 'c' in a circle to the right of 'basic functionality' we get this snippet of code:

return (function (a = 1, b = 2) { return a === 3 && b === 2; }(3));

All the code snippets are intended to be run as unit tests, returning true if supported and false otherwise.

So if you include some code like this:

function testDefaultParameters() {
  return (function (a = 1, b = 2) { return a === 3 && b === 2; }(3));
};

if (!testDefaultParameters()) {
  // log or polyfill or ...
}

That will tell you if your environment that you are running in supports this feature.

I would add these as I add new language constructs to my code base so you can detect immediately if you should add some further support or perhaps using that construct.

rasmeister
  • 1,986
  • 1
  • 13
  • 19