Special Functionality
Strict mode
In essence, "use strict" is a directive introduced in ECMAScript 5 (ES5) that
signals to the JavaScript engine that the code it surrounds should be executed
in “strict mode”. Strict mode imposes stricter parsing and error handling rules,
essentially making your code more secure and less error-prone.
When you use “use strict”, it helps you to write cleaner code, like preventing you from using undeclared variables. It can also make your code more secure because it disallows some potentially insecure actions.
How to use strict mode
-
Global Scope: To enable strict mode globally, add the directive at the beginning of the JavaScript file:
Example
"use strict"; // any code in this file will be run in strict mode function add(a, b) { return a + b; } -
Local Scope: To enable strict mode within a function, add the directive at the beginning of the function:
Example
function myFunction() { "use strict"; // this will tell JavaScript engine to use strict mode only for the `myFunction` // Anything that is outside of the scope of this function will be treated as non-strict mode unless specified to use strict mode }
Key features of strict mode
- Error prevention : Strict mode prevents common errors such as:
- Using undeclared variables.
- Assigning values to non-writable properties.
- Using non-existent properties or variables.
- Deleting undeletable properties.
- Using reserved keywords as identifiers.
- Duplicating parameter names in functions.
- Improved security: Strict mode helps in writing more secure code by:
- Preventing the use of deprecated features like arguments.caller and arguments.callee.
- Restricting the use of eval() to prevent variable declarations in the calling scope.
- Compatibility : Strict mode ensures compatibility with future versions of JavaScript by preventing the use of reserved keywords as identifiers.
Examples
Preventing accidental creation of global variables
// Without strict mode
function defineNumber() {
count = 123;
}
defineNumber();
console.log(count); // logs: 123"use strict"; // With strict mode
function strictFunc() {
"use strict";
strictVar = 123; // ReferenceError: strictVar is not defined
}
strictFunc();
console.log(strictVar); // ReferenceError: strictVar is not definedMaking assignments which would otherwise silently fail to throw an exception
// Without strict mode
NaN = "foo"; // This fails silently
console.log(NaN); // logs: NaN"use strict"; // With strict mode
NaN = "foo"; // TypeError: Assignment to read-only properties is not allowed in strict modeMaking attempts to delete undeletable properties throw an error in strict mode
// Without strict mode
delete Object.prototype; // This fails silently"use strict"; // With strict mode
delete Object.prototype; // TypeError: Cannot delete property 'prototype' of function Object() { [native code] }Is it “strictly” necessary?
Adding 'use strict' in JavaScript is still beneficial and recommended, but it
is no longer strictly necessary in all cases:
- Modules: The entire contents of JavaScript modules are automatically in
strict mode, without needing the
'use strict'statement. This applies to ES6 modules as well as Node.js CommonJS modules. - Classes: Code within class definitions is also automatically in strict
mode, even without
'use strict'.
While 'use strict' is no longer mandatory in all contexts due to the automatic
strict mode enforcement in modules and classes, it is still widely recommended
as a best practice, especially for core JavaScript files, libraries, and when
working with older browser environments or legacy code.
Notes
- Placement: The
'use strict'directive must be placed at the beginning of the file or function. Placing it anywhere else will not have any effect. - Compatibility: Strict mode is supported by all modern browsers except Internet Explorer 9 and lower.
- Irreversible: There is no way to cancel
'use strict'after it’s being set.