AceDevHub
Intermediate JavaScript Interview QuestionsIntermediateConcept

JavaScript · Question 43

What does strict mode change in JavaScript, and when is it enabled automatically?

Direct answer

Strict mode opts code into stricter JavaScript semantics, preventing some error-prone behavior; ES modules and class bodies are strict automatically, while classic script/function code can use the "use strict" directive.

Strict mode was introduced to make several historically permissive behaviors fail clearly instead of silently creating surprising results. In classic scripts, it can be enabled with a directive such as 'use strict';.

  • Assigning to an undeclared identifier no longer creates an accidental global; it throws a ReferenceError.
  • In a plain function call, this remains undefined instead of being substituted with the global object.
  • Several silent assignment failures become errors, which makes bugs easier to detect.
  • The with statement is not allowed, and some legacy syntax/parameter patterns are restricted.

Modern application code often uses strict semantics without an explicit directive because type="module" scripts and imported ES modules are strict by definition, as are class bodies.