5

In google-apps-script script file is it possible to set 'use strict'? I've created a function to test this

function a () {  //logs - function a
  "use strict";
  Logger.log(arguments.callee);
}

If strict mode was being enforced, I would think this statement would throw a TypeError.

Rubén
  • 34,714
  • 9
  • 70
  • 166
user1472330
  • 123
  • 1
  • 10

2 Answers2

6

Apps Script does not support strict mode on the server. If you use HtmlService, all client script code you write is implicitly strict mode whether or not you specify "use strict".

Corey G
  • 7,754
  • 1
  • 28
  • 28
  • Was this still the case with HtmlService IFRAME mode? Think you wrote this when that option wasn't out yet. – Bryan P Jan 22 '16 at 23:21
  • 1
    As of August 2018, it looks like Apps Script supports strict mode to some extent: `"use strict"; foo = 1;` on the top level produces an error - the script won't run. In a function, `"use strict"` seems to do nothing: `function bar() { "use strict"; foo = 1; }` does not cause an error and sets the global variable `foo`. Also, top-level strict mode seems to have no effect on functions - they are still in sloppy mode. :-( – jcsahnwaldt Reinstate Monica Aug 10 '18 at 12:40
1

Not possible on the old runtime (Rhino) but it's possible on the new runtime (V8).

There are reports of scripts that used to work on the old runtime that are not working on the new runtime, so plan to do some tests before applying this change on "production".

Resources

Rubén
  • 34,714
  • 9
  • 70
  • 166