HuntExams Academy logo
JavaScriptIntermediate#fundamentals

What is strict mode?

Answer

'use strict' (implicit in modules and classes) forbids undeclared variables, makes silent failures throw, sets this to undefined in plain function calls and disallows duplicate parameter names.

Example

'use strict';
x = 5; // ReferenceError instead of creating a global

Detailed explanation

Strict mode is a restricted version of JavaScript execution that catches certain mistakes and removes some confusing legacy behaviour. Enable it by placing the string directive 'use strict'; at the beginning of a script or function body, before ordinary statements. It applies to that scope and code defined inside it, not automatically to every script on the page. JavaScript modules and class bodies are already strict, so they do not need the directive. Strict mode is not static typing or a separate JavaScript version; it changes specific language rules.

A key benefit is preventing accidental global variables. In strict code, 'use strict'; x = 5; throws a ReferenceError if x has no existing binding, instead of creating a global property as sloppy code normally would. Certain operations that otherwise fail silently also throw: assigning to a non-writable property, assigning to a getter-only property, or deleting a non-configurable property produces a TypeError. These rules expose bugs nearer their source, which helps when maintaining applications and debugging unexpected state changes. However, strict mode does not turn every unsuccessful operation into an exception.

Strict mode also changes how function calls handle this. Inside an ordinary strict function called directly as fn(), this is undefined rather than being replaced with the global object. Method calls such as obj.fn() still receive obj as their receiver. With call() or apply(), a strict function preserves an explicitly supplied this value, including null, undefined, and primitive values. Arrow functions remain different: they capture this from their surrounding lexical scope rather than receiving it from the call.

Other restrictions are checked while parsing: strict code rejects duplicate parameter names, with statements, and deleting a plain identifier. It also prevents eval-created declarations from leaking into the surrounding scope and stops the arguments object from automatically tracking changes to named parameters. Interviewers typically expect you to distinguish parse-time SyntaxErrors from runtime errors and explain the scope of the directive. A strong answer connects these rules to safer code without claiming that strict mode guarantees correctness, provides security isolation, or always improves performance.

Key points to remember

  • Enable strict mode with a leading 'use strict'; directive in a script or function body.
  • Modules and class bodies are implicitly strict; ordinary scripts are not.
  • Assignment to an unresolvable identifier throws ReferenceError instead of creating an accidental global.
  • Certain silent property-operation failures become TypeErrors, and duplicate parameter names are rejected.
  • A directly called ordinary strict function receives undefined as this; method receivers and arrow-function rules still apply.

Common mistakes candidates make

  • Placing 'use strict'; after ordinary statements and assuming it enables strict mode.
  • Saying this is always undefined in strict mode, ignoring method calls, explicit receivers, and arrows.
  • Assuming strict mode fixes every JavaScript pitfall or makes every failed operation throw.

Likely follow-up questions

How does strict mode affect arguments?

In a strict function, named parameters and arguments entries do not automatically stay synchronised. In function f(a) { 'use strict'; a = 10; return arguments[0]; }, calling f(2) returns 2.

Can every function contain a 'use strict' directive?

No: placing that directive inside a function with default, rest, or destructured parameters is a SyntaxError. Such a function can still be strict by being defined within strict code or a module.

Does strict mode affect functions called from strict code?

A strict caller does not make an externally defined sloppy function strict. The called function's own definition context and directives determine its strictness.

Interview tip: Define strict mode, show the undeclared-variable example, then explain this binding and mention that modules and class bodies enable it automatically.

Related Interview Questions

1
JavaScriptBeginner#fundamentals#types

What are the data types in JavaScript?

Open
2
JavaScriptBeginner#scope#es6

Difference between var, let and const?

Open
3
JavaScriptBeginner#scope

What is hoisting?

Open