JavaScript Use Strict
12 April 2025 | Category: JavaScript
JavaScript is a flexible and forgiving language, but this flexibility can sometimes lead to unexpected behaviors or silent bugs. To enforce stricter parsing and error handling in your JavaScript code, ECMAScript 5 introduced the "use strict"
directive.
In this guide, you’ll learn:
- What
"use strict"
does - Why it’s important
- How to use it properly
- What behaviors it changes
- Real-world examples
What Is "use strict"
?
"use strict"
is a pragma, a special instruction to the JavaScript engine to execute the code in Strict Mode. Strict mode changes how JavaScript executes code to avoid silent errors, enforce cleaner syntax, and prepare for future versions of the language.
It does not affect the logic of your code but enforces a stricter set of rules.
How to Use "use strict"
You can apply strict mode in two ways:
1. Globally (Entire Script)
Add it at the very top of your file:
"use strict";
let num = 10;
console.log(num);
2. Locally (Function Scope)
Apply it to individual functions:
function show() {
"use strict";
message = "Hello"; // ReferenceError: message is not defined
}
Note:
"use strict"
must be the first statement in the file or function to take effect.
What Changes in Strict Mode?
Here’s what strict mode does differently:
1. Prevents Undeclared Variables
Without strict mode, assigning a value to an undeclared variable creates a global variable:
message = "Hello"; // No error (not ideal)
In strict mode:
"use strict";
message = "Hello"; // ❌ ReferenceError
2. Eliminates Silent Failures
Some actions that fail silently in non-strict mode throw errors in strict mode. For example:
"use strict";
Object.defineProperty(this, "x", { value: 42, writable: false });
x = 9; // ❌ TypeError: Cannot assign to read only property
3. Disallows Duplicates
Strict mode disallows duplicate parameter names in function definitions:
"use strict";
function duplicate(a, a) { // ❌ SyntaxError
return a;
}
4. Makes this
Safer
In non-strict mode, this
inside a function (not a method) refers to the global object. In strict mode, it becomes undefined
.
function show() {
"use strict";
console.log(this); // undefined
}
show();
5. Reserved Words Restrictions
Strict mode reserves keywords for future use. You can’t use them as variable names. Examples include: implements
, interface
, let
, package
, private
, protected
, public
, static
, yield
.
"use strict";
let interface = "value"; // ❌ SyntaxError
Common Errors Caught by Strict Mode
Behavior | Without "use strict" | With "use strict" |
---|---|---|
Undeclared variable | Creates global var | ReferenceError |
Writing to read-only prop | Fails silently | TypeError |
Duplicate function arguments | Allowed | SyntaxError |
this in standalone function | Global object (window ) | undefined |
When Should You Use It?
- In all modern JavaScript development.
- When writing modules (strict mode is automatically applied in ES6 modules).
- In functions that require strict behavior (like API logic or utilities).
Browser Support
All modern browsers, including Chrome, Firefox, Safari, Edge, and Internet Explorer 10+, fully support strict mode.
Best Practices
- Always use
"use strict"
in new scripts. - Place it at the top of the script or function.
- Avoid mixing strict and non-strict code in the same file.
- Use linting tools like ESLint to catch common errors that
"use strict"
would detect.
Conclusion
Strict mode is a powerful feature in JavaScript that helps you write cleaner, more secure, and error-resistant code. By enabling "use strict"
, you take a step toward better practices, more maintainable code, and fewer surprises during runtime.
It’s simple to use and widely supported—so start using it today in your JavaScript files!