Is "use strict" required to be in every java script file in AEM? | Community
Skip to main content
Anderson_Hamer
Level 4
July 16, 2021
Solved

Is "use strict" required to be in every java script file in AEM?

  • July 16, 2021
  • 2 replies
  • 1901 views
The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode you can not use undeclared variables.

My question is, do we need to have this in every piece of custom Javascript file? Can it be set at global level or can we ask AEM to use "use strict" for all Javascript files at global or instance level?
 
(function($,document) {
"use strict";
...
})(Granite.$, document);
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar
 

"use strict" applies only to function or program scope. So if you have fileA.js with "use strict" at the top, fileA.js executes in strict mode, and all functions defined in it will do the same when called. But fileB.js is a separate program, so the "use strict" from fileA.js doesn't apply to it.

 

The entire file :

<script...>
  "use strict";

or

The entire function and its embedded functions for example :

function fn(){
  "use strict";

 

Check this for more detail before applying for the entire file

https://stackoverflow.com/questions/2343608/in-ecmascript5-whats-the-scope-of-use-strict

 

 

2 replies

Shubham_borole
Community Advisor
Community Advisor
July 17, 2021

Hi,

 

In any custom file where we need the strict scope to be applied, the 'use strict' must be added there itself. So far not came across a global way to do that. We can see if Adobe staff can confirm.

 

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
July 17, 2021
 

"use strict" applies only to function or program scope. So if you have fileA.js with "use strict" at the top, fileA.js executes in strict mode, and all functions defined in it will do the same when called. But fileB.js is a separate program, so the "use strict" from fileA.js doesn't apply to it.

 

The entire file :

<script...>
  "use strict";

or

The entire function and its embedded functions for example :

function fn(){
  "use strict";

 

Check this for more detail before applying for the entire file

https://stackoverflow.com/questions/2343608/in-ecmascript5-whats-the-scope-of-use-strict

 

 

Arun Patidar