What is Strict Mode in JavaScript?
Learn what JavaScript strict mode is, how to enable it with 'use strict', how it catches silent errors, changes 'this', with clear examples and answers.
Expected Interview Answer
Strict mode is an opt-in restricted variant of JavaScript, enabled with the 'use strict' directive, that catches common coding mistakes and unsafe actions by turning silent errors into thrown exceptions.
You enable it by placing the string 'use strict'; at the top of a script or a function, and it applies automatically inside ES modules and class bodies. It forbids things like assigning to undeclared variables, using reserved future keywords, and duplicate parameter names, and it changes 'this' inside plain function calls from the global object to undefined. The goal is to make code more secure, easier to optimise, and less prone to hard-to-find bugs.
- Turns silent failures into loud, catchable errors
- Prevents accidental creation of global variables
- Makes 'this' undefined in plain function calls instead of the global object
- Disallows unsafe and deprecated syntax
- Helps JavaScript engines optimise code
AI Mentor Explanation
Strict mode is like playing with the third umpire and DRS switched on. In a casual gully match a faint edge might be ignored and play continues, but under strict officiating that same borderline mistake is reviewed and called out immediately, so errors that would slip by silently are caught the moment they happen.
Step-by-Step Explanation
Step 1
Add the directive
Place the exact string 'use strict'; at the very top of a script or the first line inside a function.
Step 2
Understand the scope
It applies to the whole script when at file top, or only to one function when placed inside it.
Step 3
Know the automatic cases
ES modules and the bodies of classes run in strict mode by default, so no directive is needed there.
Step 4
Observe the new errors
Assigning to an undeclared variable, writing to a read-only property, or duplicating parameters now throws instead of failing silently.
Step 5
Check 'this' behaviour
Inside a plain function call, 'this' is undefined rather than the global object, preventing accidental global mutation.
What Interviewer Expects
- Knowing how to enable strict mode with the directive
- Understanding it turns silent errors into thrown exceptions
- Awareness that modules and classes are strict by default
- The change to 'this' in plain function calls
- Concrete examples of what strict mode forbids
Common Mistakes
- Thinking strict mode is on by default everywhere
- Placing 'use strict' after other statements where it has no effect
- Confusing strict mode with TypeScript's strict compiler flags
- Not knowing that 'this' becomes undefined in plain functions
- Assuming it changes runtime performance rather than error handling
Best Answer (HR Friendly)
“Strict mode is a setting you switch on in JavaScript so the language becomes fussier and points out mistakes instead of quietly ignoring them. You turn it on by adding the line 'use strict' at the top, and it helps catch bugs earlier and write safer code.”
Code Example
'use strict';
function updateCount() {
// Typo: 'conut' was never declared with let/const/var
conut = 5; // ReferenceError in strict mode instead of creating a global
return conut;
}
try {
updateCount();
} catch (err) {
console.log(err.name); // 'ReferenceError'
}
// 'this' is undefined in a plain call under strict mode
function whoAmI() {
return this;
}
console.log(whoAmI()); // undefined (not the global object)Follow-up Questions
- How do you enable strict mode for just a single function?
- Why are ES modules always in strict mode?
- What happens to 'this' inside a plain function under strict mode?
- Name three things strict mode forbids that sloppy mode allows.
- Does strict mode affect performance, and if so how?
MCQ Practice
1. How do you enable strict mode for an entire script?
Placing the directive 'use strict'; as the first statement of a script enables strict mode for the whole file.
2. In strict mode, what is 'this' inside a plain function call?
Strict mode sets 'this' to undefined in plain function calls, preventing accidental access to the global object.
3. Which of these is NOT automatically in strict mode?
Plain scripts are in sloppy mode unless you add 'use strict'; modules and class bodies are strict by default.
Flash Cards
How do you turn on strict mode? — Add the directive 'use strict'; at the top of a script or as the first line inside a function.
Where is strict mode automatic? — Inside ES modules and class bodies — no directive required.
What does strict mode do to undeclared assignments? — Throws a ReferenceError instead of silently creating a global variable.
What is 'this' in a plain strict-mode function call? — undefined, rather than the global object.