Is 'use strict' necessary?

Is it still necessary to add “use strict”; in a GJS script, like an extension or a program, or today any code is already run in strict mode “automagically”?

I believe if you’re using ESM, this is non-optional (i.e. it is turned on automatically). Otherwise, it can make a difference, but these days they’re generally obvious mistakes.

For example, if you use 'use strict'; with the code below, you will get a critical warning and throw an exception, otherwise you will get a global variable:

// 'use strict';

function foo() {
  bar = 'baz';
}

Yes, and it’s the same for class bodies. That is, most code should already be covered before porting to ESM.

1 Like

@andyholmes @fmuellner No, no… You didn’t understand. My question is: do I still have (as of December, 2022) to write “use strict” at the beginning of all of my javascript files, when writing a GJS/GTK application, to get all the bells and whistles of the strict mode? Or is it automatically enabled in all programs, and it is only required in web pages?

The short answer is: Yes if you use gjs’ legacy imports, no if you use modules.

1 Like

Simply put; yes you need ‘use stict’ unless the program started with gjs -m (ES modules)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.