All topics
Fundamentalsbeginner

Strict Mode Explained

The opt-in stricter variant of JavaScript that catches silent errors, disables risky features, and is the default inside ES modules and classes.

Strict mode is an opt-in mode, enabled with the string 'use strict' at the top of a file or function, that changes JavaScript's behavior to catch mistakes that would otherwise fail silently, throw more helpful errors, and disable a handful of features considered unsafe or confusing. It's a smaller topic than closures or the event loop, but interviewers use it to check whether you know what modern defaults actually are.

Sloppy mode is like a lenient proofreader who silently 'fixes' your typos without telling you; strict mode is an editor who stops and flags every mistake immediately so you actually learn about it instead of shipping it.

Key Concepts

1
Without strict mode, assigning to an undeclared variable silently creates a global variable instead of throwing, which is a classic source of hard-to-trace bugs; strict mode turns this into a ReferenceError immediately. Strict mode also disallows duplicate parameter names in function signatures, disallows assigning to read-only properties or non-extensible objects (throwing instead of silently failing), and changes this inside a plain function call from defaulting to the global object to being undefined, which surfaces bugs where this was assumed to be something it wasn't.
ReferenceErrorthisundefined
2
You opt into strict mode per-file or per-function by placing the literal string 'use strict' at the very top before any other statements. Critically, you don't need to think about this for modern code most of the time: ES modules (import/export files) and the bodies of ES6 classes are automatically in strict mode, with no directive needed. This is one of the reasons plain this inside a class method is undefined rather than the global object when called without a receiver.
'use strict'importexportthisundefined
3
Understanding strict mode mostly matters for reading older non-module scripts, understanding why certain patterns throw in classes/modules but not in plain old scripts, and recognizing that 'sloppy mode' (non-strict) is now the exception rather than the rule in modern JavaScript development.