Strict mode is a way to opt into a restricted variant of JavaScript, introduced in ECMAScript 5 (ES5). It makes it easier to write “secure” JavaScript by catching common coding mistakes and preventing the use of certain unsafe actions.
Enabling Strict Mode
You can enable strict mode in two ways:
- Globally: Apply strict mode to the entire script by placing
'use strict';
at the top. - Function Scope: Apply strict mode to a specific function by placing
'use strict';
inside the function.
// Global strict mode 'use strict'; x = 3.14; // Error: x is not defined // Function-level strict mode function myFunction() { 'use strict'; y = 3.14; // Error: y is not defined }
Key Features of Strict Mode
- Eliminates
this
coercion:- In strict mode,
this
isundefined
in functions that are invoked without a specific context.'use strict'; function test() { console.log(this); // undefined } test();
- In strict mode,
- Prevents accidental globals:
- Assigning a value to an undeclared variable results in a
ReferenceError
.'use strict'; myVar = 10; // Error: myVar is not defined
- Assigning a value to an undeclared variable results in a
- Disallows duplicate parameter names:
- Function parameters must have unique names.
'use strict'; function sum(a, a) { // Error: Duplicate parameter name not allowed return a + a; }
- Function parameters must have unique names.
- Throws errors on assignments to read-only properties:
- Assigning values to non-writable properties throws a
TypeError
.'use strict'; const obj = {}; Object.defineProperty(obj, 'x', { value: 42, writable: false }); obj.x = 9; // Error: Cannot assign to read only property 'x'
- Assigning values to non-writable properties throws a
- Prohibits
delete
of plain names:delete
operations can only be used on properties.'use strict'; var x = 5; delete x; // Error: Cannot delete 'x'
- Prohibits
with
statements:- The
with
statement is disallowed in strict mode because it can make code difficult to predict and optimize.'use strict'; with (Math) { // Error: Strict mode code may not include a with statement x = random(); }
- The
- Prevents setting
eval
andarguments
as variable names:- These are reserved for special use in JavaScript.
'use strict'; var eval = 10; // Error: Unexpected eval or arguments in strict mode
- These are reserved for special use in JavaScript.
- Restricts the use of octal literals:
- Octal literals are not allowed in strict mode.
'use strict'; var num = 010; // Error: Octal literals are not allowed in strict mode
- Octal literals are not allowed in strict mode.
Benefits of Strict Mode
- Enhanced Error Checking: Makes it easier to catch common errors and bugs.
- Improved Performance: Strict mode can be optimized better by JavaScript engines.
- Safer Code: Prevents the use of some features that are considered problematic or poorly designed.
Use Cases
- New Projects: It’s a good practice to use strict mode in all new JavaScript projects.
- Learning Tool: Helps new developers avoid common pitfalls.
- Security: Makes the codebase more secure by preventing potentially dangerous actions.
Strict mode is a powerful tool to ensure cleaner and more robust JavaScript code, and it’s highly recommended to use it wherever possible.