1

I ran into a issue eventually it turned out because I did not use var prefix during declare a local variable within a function, so when it get to next function it actually automatically pick it up the variable, even though that was a syntax mistake as my initial intent is to use another local variable with similar name. So is there a setting on google apps script to throw error on a global variable declaration within function to avoid this kind of tricky issues?

Here is sample code of my problematic issue

function f1(){
   for(var idx=0;idx<length; idx++){
     tmp1 = idx;  // since tmp1 missing var, it endup as global variable
     ...
   }
}
function f2(){
   Logger.log(tmp1);// even though I did not delcare tmp1 here, it will not throw either validation nor runtime error.
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
Henry
  • 11
  • 1

1 Answers1

1

Use the JavaScript strict mode. To do this, add the following at the global scope:

'use strict';

The above string literal could be added on any file in your Google Apps Script project, just be sure to add it outside of any function. In order to make this easier to find, add it on top of your project's first file.

Resources

Related

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