"use strict" in JavaScript
In JavaScript, "use strict" is a directive that enables strict mode, introduced in ECMAScript 5 (ES5) to assist in writing safer and more error-free code. "use strict" tells the JavaScript engine to run the code in strict mode, applying stricter rules for JavaScript coding.
What is "use strict" in JavaScript?
"use strict" is a literal expression that enables strict mode for JavaScript. It is added at the top of a JavaScript file, function, or block to enforce stricter coding rules.
Syntax
To enable strict mode, add "use strict"; at the top of a script or function:
"use strict";
console.log("Strict mode is enabled");
function myFunction() {
"use strict";
let x = 3.14;
console.log(x);
}Why Use Strict Mode?
Strict mode was introduced to address JavaScript’s problematic features and encourage developers to write safer, optimized code.
1. Avoids Accidental Global Variables
"use strict";
x = 10; // Throws an error because 'x' is not declared2. Eliminates Silent Errors
"use strict";
Object.defineProperty(this, "PI", { value: 3.14159, writable: false });
PI = 3.14; // Throws an error3. Disallows Duplicates in Function Parameters
"use strict";
function sum(a, a, c) {
// Syntax error in strict mode
return a + a + c;
}4. Prevents this from Defaulting to the Global Object
"use strict";
function showThis() {
console.log(this); // Logs 'undefined'
}
showThis();5. Avoids Deletion of Non-Deletable Properties
"use strict";
delete Object.prototype; // Throws an errorHow to Use "use strict" Mode?
Strict mode can be enabled globally or within a function.
Global Strict Mode
"use strict";
let name = "Sourav";Local Strict Mode (Per Function)
function display() {
"use strict";
let message = "Hello, World!";
console.log(message);
}Common Errors in Strict Mode
1. Mistyping a Variable Name
"use strict";
x = 3.14; // Throws an error2. Using a Variable Without Declaring It
"use strict";
x = { p1: 10, p2: 20 }; // Throws an error3. Deleting a Variable or Object
"use strict";
let x = 3.14;
delete x; // Throws an error4. Duplicating a Parameter Name
"use strict";
function x(p1, p1) {}; // Throws an error5. Octal Numeric Literals
"use strict";
let x = 010; // Throws an error6. Escape Characters
"use strict";
let x = \010; // Throws an errorLimitations and Compatibility Considerations
- Not Retroactive: Adding
"use strict";does not affect previously executed code. - Old Browsers: In older browsers that do not support strict mode, the directive is ignored, ensuring backward compatibility.
- Module Scripts: In ES6 modules, strict mode is enabled by default.
When Not to Use Strict Mode?
- Working with Third-Party Code: If libraries are not built for strict mode, enabling it may introduce errors.
- Quick Prototyping: Strict mode may slow down rapid development by enforcing rules.
Conclusion
Strict mode in JavaScript, introduced in ECMAScript 5, enforces stricter parsing and error handling. It eliminates silent errors, enhances performance, disables unsafe features, and future-proofs code. Using "use strict" ensures more secure, optimized, and debuggable JavaScript code.