JavaScript Strict Mode Enforcement

In JavaScript, strict mode is enforced in modules but optional in regular programs. However, Lightning Locker and Lightning Web Security 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, or const declarations in libraries or modules.
  • To share code between components, you must export variables and functions from modules and import them into other modules. See Share JavaScript Code.
  • The libraries that your components use must also work in strict mode but don’t need to specify "use strict" in their own code. See Use Third-Party JavaScript Libraries.
  • If you use third-party libraries that specify "use strict" explicitly, you can get errors under LWS. See Third-Party Library Strict Mode With LWS.

For more information about JavaScript strict mode, see the Mozilla Developer Network.