Code mistakes used to slip by without a warning. That made bugs hard to trace and fix. JavaScript “use strict” Mode came to block this kind of silent failure.
Table of Content
- Understand the use strict Mode in JavaScript
- JavaScript Strict Mode in Functions vs Global Scope
- The Differences Between Strict Mode and Non-Strict Mode
- The
this
Keyword Behavior in Strict Mode in JavaScript - Variable and Naming Restrictions with Strict Mode in JavaScript
- Strict Mode in Modern JavaScript (ES6+ Modules & Classes)
- Browser Compatibility and Migration to Strict Mode
- Wrapping Up
- FAQs
Developers needed a way to write cleaner, safer code. That is where strict mode came in.
Understand the use strict Mode in JavaScript
The “use strict” Mode is a string. You write it at the top of your code or inside a function. That tells JavaScript to turn on strict mode rules from that point.
JavaScript was too loose. You could forget a var
and create a global variable by mistake. You could write over protected values without a warning. Strict mode forces better habits and reveals mistakes early.
Here is an example:
"use strict";
count = 10;
Output:
ReferenceError: count is not defined
Without strict mode, JavaScript adds count
to the global object. With strict mode, JavaScript throws an error. That stops a bug before it spreads.
So, How does “use strict” prevent silent errors in JavaScript?
JavaScript ignores many problems in normal mode. Strict mode throws errors when:
- You assign to a variable that you did not declare.
- You try to write to read-only properties.
- You use objects or variables in the wrong way.
You get an error instead of a broken program that looks like it works.
Strict mode keeps this
as undefined
in normal functions. That avoids confusion and keeps function scope clear.
Strict mode blocks eval()
from affecting the outside scope. Code inside eval
stays inside it.
Changes to function arguments do not sync with the arguments
object anymore. That removes weird bugs in certain cases.
JavaScript Strict Mode in Functions vs Global Scope
The strict mode works in two ways. You can use it across a whole script or limit it to one function.
"use strict"; // global strict mode
function work() {
// strict mode here too
}
For another example:
function check() {
"use strict"; // only this function uses strict mode
}
You get more control when you use it inside single functions. That helps when you update old files step by step.
The Differences Between Strict Mode and Non-Strict Mode
Strict mode makes JavaScript behave in a safer way.
- Silent errors throw real errors. You know what went wrong and where.
- It blocks global variable leaks. You must declare every variable first.
- Function parameters cannot be repeated. That keeps argument handling clear.
this
stays controlled in functions. It does not point to the global object by mistake.
Strict mode catches mistakes early and avoids messy bugs later.
The this
Keyword Behavior in Strict Mode in JavaScript
Strict mode changes what this
points to.
this
is undefined
in non-method functions:
"use strict";
function showLog() {
console.log(this); // undefined
}
showLog();
Without strict mode, this
would point to the global object. That causes bugs in large codebases.
Prevents this
coercion:
"use strict";
function show() {
return this;
}
console.log(show.call(null)); // null stays null
JavaScript in normal mode turns null
into a global object. Strict mode stops that.
Variable and Naming Restrictions with Strict Mode in JavaScript
Strict mode blocks old habits that lead to bad code.
- You cannot use
with
. This statement makes code slow and unclear. - You cannot use unsafe names.
eval
andarguments
are off-limits as variable names. - It blocks hidden mistakes. You cannot delete undeletable properties or assign them to constants.
Strict mode helps browsers optimize your code. That gives better speed and lower memory use.
Strict Mode in Modern JavaScript (ES6+ Modules & Classes)
You do not need "use strict"
in ES6 modules or class files.
// inside a module or class
export function run() {
// strict mode is already active
}
Modern features rely on strict rules. That means const
, let
, arrow functions, and classes all work better when strict mode runs.
Strict mode blends with ES6+ syntax. Most new tools expect it by default.
Browser Compatibility and Migration to Strict Mode
All major browsers support strict mode. That includes Chrome, Firefox, Safari, Edge, and Internet Explorer from version 10.
Start small. Add "use strict";
inside a single function. Fix any errors it shows. After that, move to bigger sections.
Do not apply strict mode to full files until you check for global leaks, bad names, and silent errors.
Wrapping Up
In this article, you learned how JavaScript “use strict” Mode blocks mistakes and changes how JavaScript works. You saw how it catches errors, limits bad patterns, and improves how this
, eval
, and arguments
behave.
Here is a quick recap:
- Add
"use strict";
to force safer code rules. - Use it inside a function or at the top of a file.
- It blocks undeclared variables and throws useful errors.
- It changes how
this
andarguments
behave. - ES6 modules and classes already use strict mode.
- Use it to avoid hidden bugs and help your browser run code faster.
FAQs
Should you always use strict mode?
Yes, especially for new code. It helps catch errors early and enforces better coding practices.
When might you avoid strict mode?
Avoid it when working with legacy code that relies on old JavaScript behaviors or bad practices.
How does “use strict” affect JavaScript eval and arguments?
In strict mode, eval()
can’t modify outer variables. Also, arguments
no longer reflects changes to named function parameters.
Can “use strict” improve JavaScript performance?
Yes. It can help JavaScript engines optimize code better by eliminating problematic features.
Does strict mode block all bad code?
No. It prevents many common bugs, but it doesn’t catch everything. You still need to write clean code.
What if you forget to declare a variable in strict mode?
It throws an error. You must declare variables with let
, const
, or var
.
Can you remove “use strict” in modules?
You don’t need it in ES6 modules—strict mode is enabled by default.
Will strict mode affect third-party libraries?
It can. If you enable it globally and the library depends on sloppy mode, it may break. Use it carefully with mixed code.
Does strict mode help with security?
Yes. It disables dangerous features like with
and makes code more predictable, reducing some attack vectors.
Is strict mode the same as using TypeScript?
No. Strict mode enforces some runtime rules. TypeScript adds compile-time checks and static typing.
What is "use strict" in JavaScript?
- Preventing undeclared variables
- Making assignments to read-only properties throw errors
- Disabling dangerous features like with
- Fixing this inside functions (defaults to undefined instead of window)
How to Enable Strict Mode in JavaScript
"use strict";
x = 5; // Error: x is not defined
Locally (inside a function)
Add it inside a function to limit strict mode to that function:
function run() {
"use strict";
y = 10; // Error: y is not defined
}
In ES6 modules, strict mode is enabled by default, so you don’t need "use strict".