Strict Mode ('use strict')
Strict mode makes JavaScript stricter. It catches your mistakes and shows you errors instead of hiding them.
What Is Strict Mode?
Normally JavaScript is very forgiving. If you make a small mistake, it quietly ignores it and keeps going. This sounds nice, but it hides bugs that can cause big problems later.
Strict mode turns that off. It tells JavaScript:
"Stop being forgiving. Show me my mistakes right away." You turn it
on by writing 'use strict' at the top of your file.
'use strict';
x = 10; // ❌ Error: x is not defined
// Without strict mode this would work silently — and cause hidden bugs
Normal mode is like a teacher who never corrects your spelling. You never learn your mistakes. Strict mode is a teacher who marks every mistake in red pen. It's stricter, but you actually improve.
How to Turn It On
// Whole file — put this on the very first line
'use strict';
function doSomething() {
// now the whole file uses strict mode
}
// Just one function — only that function is strict
function strictOnly() {
'use strict';
// only inside here
}
Put it at the very top. If you put it in the middle, only the code below it gets strict mode — code above it stays normal.
If you use import / export (ES Modules)
or write code inside a class, strict mode is on
automatically. You don't need to write it.
What Does It Actually Change?
| Without strict mode | With strict mode |
|---|---|
| Using an undeclared variable silently creates a global | ❌ Throws a ReferenceError |
| Deleting a variable does nothing | ❌ Throws a SyntaxError |
| Two function parameters with the same name is allowed | ❌ Throws a SyntaxError |
this inside a plain function is the global
window
|
this is undefined |
| Writing to a read-only property fails silently | ❌ Throws a TypeError |
The this Keyword Change
'use strict';
function whoAmI() {
console.log(this);
}
whoAmI(); // undefined — not the window object
Without strict mode, this inside a regular function
points to the whole browser window. That's risky — you could
accidentally change global things. Strict mode makes
this undefined instead, so the mistake is
obvious right away.
No More Silent Failures
In normal mode, some mistakes simply do nothing — no error, no warning, just silence. Strict mode makes those silent failures throw an error so you can actually see them.
// Without strict mode — assigning to undeclared variable silently creates a global
undeclaredVar = 42; // no error — dangerous hidden bug
// With strict mode — same code throws an error immediately
'use strict';
undeclaredVar = 42; // ❌ ReferenceError: undeclaredVar is not defined
Real-World Examples
// Without strict mode — JavaScript silently creates a global variable
// Without strict mode — typo silently creates an accidental global variable
totalCost = 99.99; // forgot "let/const" — no error! dangerous hidden bug
// With strict mode — the same typo is caught immediately
'use strict';
totalCost = 99.99; // ❌ ReferenceError: totalCost is not defined
Try It: Code Runner
See what strict mode catches. Try removing
'use strict' from the function and observe the
difference!
Common Mistakes
-
Not putting
'use strict'on the very first line — any code before it runs in normal mode. -
Thinking you don't need it — if you're writing plain
.jsfiles (not modules), you do. - Mixing strict and non-strict files with old tools — modern bundlers like Vite handle this for you.
Best Practices
- Always use strict mode in new projects. It catches real bugs.
- If you use ES Modules or a modern bundler, it's already on — no need to write it.
Browser Compatibility
Works in every browser and Node.js since 2012. Safe to use everywhere.
Interview Questions
What does 'use strict' do?
It turns on a stricter version of JavaScript that shows errors instead of silently ignoring mistakes. It prevents things like using undeclared variables or duplicate parameter names.
Do you need 'use strict' in ES Module files?
No. ES Modules are always in strict mode automatically.
✏️ Exercise
Add 'use strict' to the top of a file. Try writing
foo = 5 without declaring foo first.
Open the console and see the ReferenceError. Then remove
'use strict' and notice it works silently.
🧠 Quiz
1. What is this inside a plain function call in strict mode?
undefined — not the global window object.
2. Where must 'use strict' go to apply to the whole file?
The very first line of the file, before any other code.
🚀 Mini Challenge
Create a function strictTest() with
'use strict' inside. Try three things: assign to an
undeclared variable, delete a variable, use two parameters with
the same name. Wrap each in try...catch and log which
error each one throws.
Summary
-
'use strict'makes JavaScript show your mistakes instead of hiding them. - Put it on the very first line of your file or function.
- ES Modules and class bodies are always strict — no need to write it.
- It's a good habit. Always use it.
Related Topics
MDN reference: developer.mozilla.org → JavaScript → Strict mode