What is `'use strict';` in JavaScript for?
What are the advantages and disadvantages to using it?TL;DR
'use strict'
is a statement used to enable strict mode to entire scripts or individual functions. Strict mode is a way to opt into a restricted variant of JavaScript.
Advantages
- Makes it impossible to accidentally create global variables.
- Makes assignments which would otherwise silently fail to throw an exception.
- Makes attempts to delete undeletable properties throw an exception (where before the attempt would simply have no effect).
- Requires that function parameter names be unique.
this
isundefined
in the global context.- It catches some common coding bloopers, throwing exceptions.
- It disables features that are confusing or poorly thought out.
Disadvantages
- Many missing features that some developers might be used to.
- No more access to
function.caller
andfunction.arguments
. - Concatenation of scripts written in different strict modes might cause issues.
Overall, the benefits outweigh the disadvantages and there is not really a need to rely on the features that strict mode prohibits. We should all be using strict mode by default.
What is "use strict"
in JavaScript?
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:
-
Local Scope: To enable strict mode within a function, add the directive at the beginning of the function:
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:
-
Making assignments which would otherwise silently fail to throw an exception:
-
Making attempts to delete undeletable properties throw an error in strict mode:
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.