Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What does "use strict" do in JavaScript, and what is the reasoning behind it?
In this tutorial, we will learn what "use strict" does in JavaScript and why it's important for writing safer code.
The "use strict" directive enables strict mode in JavaScript, which was introduced in ECMAScript 5 (ES5). It enforces stricter parsing and error handling, helping developers catch common mistakes and write more secure code.
What is Strict Mode?
Strict mode changes both syntax and runtime behavior. It eliminates some JavaScript silent errors by changing them into throw errors, fixes mistakes that make it difficult for JavaScript engines to perform optimizations, and prohibits some syntax likely to be defined in future versions of ECMAScript.
Syntax
"use strict"; // Your code here
You can enable strict mode globally by placing it at the beginning of a script, or locally within a function.
Global Strict Mode Example
<html>
<body>
<h2>JavaScript Strict Mode Demo</h2>
<p id="output"></p>
<script>
"use strict";
try {
// This will throw an error in strict mode
undeclaredVariable = "This will cause an error";
} catch (error) {
document.getElementById("output").innerHTML =
"Error caught: " + error.message;
}
</script>
</body>
</html>
Function-Level Strict Mode
<html>
<body>
<h2>Function-Level Strict Mode</h2>
<p id="demo"></p>
<script>
// Normal mode - this works
normalVar = "I'm global";
function strictFunction() {
"use strict";
try {
strictVar = "This will throw an error";
} catch (error) {
document.getElementById("demo").innerHTML =
"Normal mode: " + normalVar + "<br>" +
"Strict mode error: " + error.message;
}
}
strictFunction();
</script>
</body>
</html>
Common Strict Mode Restrictions
| Restriction | Normal Mode | Strict Mode |
|---|---|---|
| Undeclared variables | Creates global variable | Throws ReferenceError |
| Duplicate parameters | Allowed | SyntaxError |
| Octal literals | Allowed | SyntaxError |
| Delete variables | Ignored | SyntaxError |
Key Restrictions in Detail
1. Variables must be declared:
"use strict"; myVar = 10; // ReferenceError: myVar is not defined
2. No duplicate parameter names:
"use strict";
function myFunc(a, a) { // SyntaxError
return a + a;
}
3. Cannot delete variables, functions, or arguments:
"use strict"; var x = 10; delete x; // SyntaxError
4. Cannot write to read-only properties:
"use strict";
var obj = {};
Object.defineProperty(obj, "prop", {value: 10, writable: false});
obj.prop = 20; // TypeError
Benefits of Using Strict Mode
Strict mode provides several advantages:
- Error Prevention: Catches common coding mistakes early
- Security: Prevents accidental creation of global variables
- Performance: Enables JavaScript engines to optimize better
- Future-proofing: Prepares code for future JavaScript versions
Browser Compatibility
Strict mode is supported by all modern browsers. Older browsers that don't support it will simply ignore the "use strict" directive as a string literal, ensuring backward compatibility.
Conclusion
Using "use strict" is a best practice that helps you write cleaner, more secure JavaScript code. It catches errors early and prevents common programming mistakes, making your applications more reliable and maintainable.
