JavaScript Strict Mode Enforcement
In JavaScript, strict mode is enforced in modules but optional in regular programs. However, Lightning Web Security (LWS) and Lightning Locker implicitly enable JavaScript strict mode everywhere. You don’t need to specify "use strict"
in your code. JavaScript strict mode makes code more secure, robust, and supportable.
When strict mode is enabled and unsafe actions are taken, JavaScript throws errors that would otherwise be suppressed. Examples of unsafe actions include assigning values to non-writable properties and using a variable that hasn’t been declared. Reporting these actions can catch situations when a variable name has been mistyped.
A few common stumbling points when using strict mode are:
- You must declare all variables. Use any of the
var
,let
, orconst
declarations in libraries or modules. - To share code between components, you must follow the steps outlined in Share JavaScript Code for Lightning web components and Sharing JavaScript Code Across Components for Aura components, respectively.
- The libraries that your components use must also work in strict mode but don’t need to specify
"use strict"
in their own code. For developing Lightning web components, see Use Third-Party JavaScript Libraries. For developing Aura components, see Using External JavaScript Libraries. - If you use third-party libraries that specify
"use strict"
explicitly, you can get errors under LWS. See Third-Party Library Considerations for LWS.
For more information about JavaScript strict mode, see the Mozilla Developer Network.