Strict mode in JavaScript
Last Updated :
08 Jul, 2024
The “use strict” directive, introduced in ECMAScript version 5, is a literal expression rather than a statement and is ignored by earlier JavaScript versions. Its purpose is to enforce strict mode, ensuring the code is executed with stricter parsing and error handling. In strict mode, you cannot use undeclared variables, among other enforced rules. Using “use strict”; eliminates silent errors, enhances performance, disables unsafe features, and makes code more secure and easier to debug.
The benefits of Using strict
Strict mode makes several changes to normal JavaScript semantics.Â
- Eliminates Silent Errors: Converts JavaScript silent errors into throw errors, making debugging easier.
- Optimization: Helps JavaScript engines perform optimizations, potentially making strict mode code run faster than non-strict code.
- Future-Proofing: Prohibits syntax likely to be defined in future ECMAScript versions.
- Prevents Unsafe Actions: Throws errors when relatively “unsafe” actions are taken, such as accessing the global object.
- Disables Confusing Features: Disables features that are confusing or poorly designed.
- Security: Makes it easier to write “secure” JavaScript
How to Use Strict Mode
Strict mode can be used in two ways, remember strict mode doesn’t work with block statements enclosed in {} braces.Â
- Global Scope: Applies to the entire script.
- Function Scope: Applies to individual functions.
Using Strict Mode for the Entire Script
To invoke strict mode for an entire script, put the exact statement “use strict”; (or ‘use strict’;) before any other statements.Â
// Whole-script strict mode syntax
'use strict';
let v = "strict mode script!";
Enable strict mode on a function-by-function basis to avoid issues when concatenating strict and non-strict scripts, ensuring compatibility and preventing unintended strict mode application in the entire script.
Using Strict Mode for a Function
Likewise, to invoke strict mode for a function, put the exact statement “use strict”; (or ‘use strict’;) in the function’s body before any other statements.
function strict() {
// Function-level strict mode syntax
'use strict';
function nested() { return 'Javascript on GeeksforGeeks'; }
return "strict mode function! " + nested();
}
function notStrict() { return "non strict function"; }Examples of using Strict mode
Example: Mistyping a Variable Name
In strict mode, assigning a value (3.14) to an undeclared variable (x) without prior declaration throws an error.
JavaScript
// Using a variable, without declaring it, is not allowed:
'use strict';
x = 3.14; // will throw an error
Output:Â

Example: Using a Variable Without Declaring It
In strict mode, assigning a value to an undeclared variable (x) as an object ({p1:10, p2:20}) without prior declaration throws an error.
JavaScript
// Objects are variables too.
// Using an object, without declaring it, is not allowed:
'use strict';
// Will throw an error
x = { p1: 10, p2: 20 };
Output:Â

Example: Deleting a Variable or Object
In strict mode, attempting to delete a variable (delete x;) or function (delete x; where x is a function) declaration throws an error.
JavaScript
'use strict';
let x = 3.14;
// Deleting a function is also not allowed
'use strict';
function x(p1, p2) {};
// Will throw an error
delete x;
Output:Â

Example: Duplicating a Parameter Name
Using ‘use strict’;, defining a function with duplicate parameter names (p1, p1) causes an error due to parameter name duplication being disallowed in strict mode functions
JavaScript
'use strict';
// Will throw an error
function x(p1, p1) {};
Output:Â

Example: Octal Numeric Literals
Here we Using ‘use strict’;, 010 is interpreted as an invalid octal literal because octal literals (starting with 0) are not allowed in strict mode, causing an error when assigning to variable x.
JavaScript
'use strict';
// Will throw an error
let x = 010;
Output:Â

Example: Escape Characters
Here we Using ‘use strict’;, \010 is interpreted as an invalid octal literal, causing an error when trying to assign it to variable x.
JavaScript
'use strict';
// Will throw an error
let x = \010;
Output:Â

Example: Writing to a Read-Only Property
Using ‘use strict’; and defining obj.x as non-writable (writable:false) throws an error when attempting to assign 3.14 to obj.x, violating its non-writable property definition.
JavaScript
'use strict';
let obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
// Will throw an error
obj.x = 3.14;
Output:Â

Example: Writing to a Get-Only Property
Using ‘use strict’; disallows assigning values to read-only properties like obj.x with a getter, causing an error in the code trying to assign 3.14 to obj.x.
JavaScript
'use strict';
let obj = {get x() {return 0} };
// Will throw an error
obj.x = 3.14;
Output:Â

Example: Deleting an Undeletable Property
Using ‘use strict’; prohibits deleting properties of built-in objects like Object.prototype, which causes an error in the code snippet attempting to delete Object.prototype.
JavaScript
'use strict';
// Will throw an error
delete Object.prototype;
Output:Â

Example: Using “eval” as a Variable
Using ‘use strict’; prevents using eval as a variable name, causing an error. The code snippet demonstrates this by attempting to assign a value to eval.
JavaScript
'use strict';
// Will throw an error
let eval = 3.14;
Output:Â

Example: Using “arguments” as a Variable
Using ‘use strict’; disallows using arguments as a variable name, causing an error. The code snippet demonstrates this by attempting to assign a value to arguments.
JavaScript
'use strict';
// Will throw an error
let arguments = 3.14;
Output:Â

Example: The with Statement
Using ‘use strict’; prevents with statements, causing an error if used. The code snippet demonstrates this by attempting a with statement, which throws an error.
JavaScript
'use strict';
// Will throw an error
with (Math){x = cos(2)};
Output:Â

Note: In function calls like f(), this value was the global object. In strict mode, it is now undefined. In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties.
Conclusion
Strict mode in JavaScript, introduced in ECMAScript version 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.