What Is Strict Mode in JavaScript?
Learn what “use strict” does in JavaScript, why it throws errors instead of failing silently, and how it changes this binding.
Expected Interview Answer
Strict mode is an opt-in restricted variant of JavaScript, enabled with the literal string “use strict”, that turns silent mistakes into thrown errors, disables several confusing legacy features, and closes off patterns the engine cannot safely optimize.
Without strict mode, assigning to an undeclared variable silently creates a global, duplicate function parameters are allowed, and “this” inside a plain function call defaults to the global object. Strict mode throws a ReferenceError for the undeclared assignment, forbids the duplicate parameter, and leaves “this” as undefined in a plain call so bugs surface immediately instead of corrupting global state. It also disallows the “with” statement, blocks assignment to read-only or non-extensible properties (throwing instead of failing silently), and requires unique property names in object literals. ES modules and class bodies are strict by default, so most modern code already runs under these rules without an explicit directive.
- Converts silent failures into immediate, debuggable errors
- Prevents accidental creation of global variables
- Removes “this” defaulting to the global object in plain calls
- Blocks unsafe legacy syntax like “with” and duplicate parameters
AI Mentor Explanation
Non-strict JavaScript is like an umpire who lets a batter step outside the crease without calling it and just quietly adjusts the record. Strict mode is an umpire who calls it immediately, every time, so the batter learns instantly instead of a scorer discovering the error at the end of the innings. The rule set does not change what a legal shot is, it only changes whether violations get flagged the moment they happen. That immediate-flag-instead-of-silent-correction is exactly what “use strict” does to sloppy assignments and undeclared globals.
Step-by-Step Explanation
Step 1
Add the directive
Place “use strict” as the first statement in a script or function body (or use ES modules, which are strict by default).
Step 2
Engine parses in strict grammar
The parser rejects illegal strict-mode syntax like duplicate parameters or “with” before execution even begins.
Step 3
Runtime enforces stricter checks
Undeclared assignments throw ReferenceError; writes to read-only properties throw TypeError instead of failing silently.
Step 4
"this" binding changes
A plain function call has “this” as undefined instead of defaulting to the global object.
What Interviewer Expects
- Clear explanation of what silently fails in non-strict mode vs throws in strict mode
- Mention that ES modules and classes are strict by default
- Awareness of the “this” binding difference in plain function calls
- At least one concrete example of a strict-mode-only thrown error
Common Mistakes
- Saying strict mode changes language syntax broadly rather than specific footguns
- Forgetting that modules/classes are already strict without a directive
- Confusing strict mode with TypeScript type checking
- Not knowing that strict mode must be the first statement to take effect
Best Answer (HR Friendly)
“Strict mode is a setting you turn on in JavaScript that makes the language less forgiving of common mistakes, like accidentally creating a global variable. Instead of silently doing something possibly wrong, the code throws an error right away, which makes bugs much easier to catch early.”
Code Example
// Without strict mode: creates an accidental global
function setTotal() {
total = 100 // no 'let'/'const' — silently becomes window.total
}
setTotal()
console.log(total) // 100, leaked global
// With strict mode: throws immediately
'use strict'
function setTotalStrict() {
total2 = 100 // ReferenceError: total2 is not defined
}
setTotalStrict()Follow-up Questions
- Why are ES modules automatically in strict mode?
- What happens to “this” inside a plain function call under strict mode?
- Give an example of code that is valid without strict mode but throws with it.
- Does strict mode affect performance, and if so how?
MCQ Practice
1. What happens when you assign to an undeclared variable in strict mode?
Strict mode disallows implicit global creation and throws a ReferenceError instead.
2. What is the value of “this” inside a plain function call under strict mode?
Strict mode leaves “this” as undefined rather than defaulting to the global object.
3. Which code contexts are strict by default without needing a directive?
ES modules and the bodies of class declarations/expressions are always executed in strict mode.
Flash Cards
How do you enable strict mode? — Add “use strict” as the first statement in a script or function, or use ES modules.
What does strict mode do to undeclared assignments? — Throws a ReferenceError instead of silently creating a global.
"this" in a plain call under strict mode? — undefined, instead of defaulting to the global object.
Which contexts are strict by default? — ES modules and class bodies.