JavaScript is a flexible and forgiving language, but that flexibility sometimes leads to silent errors and unpredictable behavior. To improve code reliability, strict mode was introduced in ECMAScript 5 (ES5).
By adding 'use strict'
at the beginning of a JavaScript file or function, developers can enforce stricter parsing rules, reducing common errors and improving performance.
In this guide, we’ll cover:
✅ What 'use strict'
does
✅ The key benefits of using strict mode
✅ Common mistakes it helps prevent
✅ How to enable strict mode in your code
What Does 'use strict'
Do?
Strict mode makes JavaScript behave in a more secure and predictable way. It prevents common coding mistakes and disallows certain unsafe practices that could lead to errors or security vulnerabilities.
How to Enable Strict Mode
Strict mode can be applied in two ways:
1. For the Entire Script
'use strict';
function myFunction() {
x = 10; // ReferenceError: x is not defined
}
myFunction();
Here, 'use strict'
applies to all code in the file, preventing accidental global variables.
2. For a Specific Function
function myStrictFunction() {
'use strict';
y = 20; // ReferenceError: y is not defined
}
function myNonStrictFunction() {
z = 30; // Allowed (if not in strict mode)
}
myStrictFunction();
myNonStrictFunction();
Using strict mode inside a function allows you to control where it applies.
Key Benefits of Using 'use strict'
Strict mode offers several advantages, making it a best practice for modern JavaScript development.
1️⃣ Prevents Accidental Global Variables
Without strict mode, JavaScript implicitly creates a global variable when you assign a value to an undeclared variable.
function myFunction() {
myVar = 100; // Implicitly creates a global variable (BAD!)
}
myFunction();
console.log(myVar); // 100 (Accidentally global)
🔹 With strict mode:
'use strict';
function myFunction() {
myVar = 100; // ReferenceError: myVar is not defined
}
myFunction();
Strict mode forces developers to declare variables explicitly using let
, const
, or var
.
2️⃣ Catches Silent Errors
Some JavaScript mistakes fail silently without warning. Strict mode turns these into actual errors, making them easier to debug.
'use strict';
const obj = Object.freeze({ name: 'Alice' });
obj.name = 'Bob'; // TypeError: Cannot assign to read-only property
🔹 Without strict mode, this would fail silently, and obj.name
would remain "Alice"
with no error message.
3️⃣ Disallows Duplicates in Object Properties & Function Parameters
In non-strict mode, JavaScript allows duplicate properties in objects, which can cause unexpected behavior.
const user = {
name: 'Alice',
name: 'Bob' // No error (but causes confusion)
};
console.log(user.name); // "Bob"
🔹 With strict mode:
'use strict';
const user = {
name: 'Alice',
name: 'Bob' // SyntaxError: Duplicate data property in object literal
};
It also disallows duplicate function parameters, which is useful for debugging.
'use strict';
function sum(a, a) { // SyntaxError: Duplicate parameter name not allowed
return a + a;
}
4️⃣ Eliminates this
Coercion in Functions
In non-strict mode, calling a function without an explicit this
context assigns this
to the global object (window
in browsers).
function show() {
console.log(this); // Window (in browsers)
}
show();
🔹 With strict mode:
'use strict';
function show() {
console.log(this); // undefined
}
show();
This helps prevent accidental modifications to global objects.
5️⃣ Prevents the Use of Reserved Keywords
Strict mode prohibits using future reserved keywords (e.g., let
, class
, import
) as variable names.
'use strict';
let public = 10; // SyntaxError: Unexpected strict mode reserved word
This ensures forward compatibility with newer JavaScript versions.
6️⃣ Restricts the Use of eval()
and arguments
Strict mode makes JavaScript safer by restricting:
eval()
: It preventseval()
from introducing new variables into the surrounding scope.arguments
object: It disallows modifications to function parameters througharguments
.
'use strict';
eval("var a = 5;");
console.log(a); // ReferenceError: a is not defined
When Should You Use 'use strict'
?
✅ Always use strict mode in new JavaScript projects.
✅ Apply strict mode when refactoring legacy code.
✅ Use strict mode inside individual functions when working with third-party scripts.
When NOT to Use Strict Mode
🚫 If working with older JavaScript codebases, strict mode may break existing behavior.
Summary: Key Takeaways
✅ 'use strict'
improves code quality by preventing common mistakes.
✅ Eliminates silent errors, making debugging easier.
✅ Prevents accidental global variables and duplicate declarations.
✅ Restricts this
coercion to prevent unintended global modifications.
✅ Improves security by enforcing better coding practices.
Using 'use strict'
is a best practice for modern JavaScript development.
📌 Next Steps
- ✅ Try adding
'use strict'
to an existing script and see what errors appear. - ✅ Learn more about strict mode from MDN’s documentation.
- ✅ Comment below with any questions! 🚀