If you program in JavaScript, you may have encountered the directive "use strict"
.
But what exactly is strict mode, and why should you use it?
In this article, Iβll explain what "use strict"
is, how to enable it, and why itβs important for your code.
Letβs dive in! π
Introduction to 'use strict' π―
JavaScript is a language that has evolved over the years while maintaining backward compatibility.
This means newer versions of the language avoid introducing changes that might break older code. However, over time, it became clear that some decisions made early in the language's development were less than ideal.
To improve code quality and security, strict mode was introduced in ECMAScript 5 (ES5).
Strict mode changes how JavaScript code is interpreted, fixing certain bugs and shortcomings.
How to Enable Strict Mode? π
To enable strict mode, include the directive:
"use strict";
This simple directive tells the JavaScript engine to execute the code in strict mode.
Remember, the directive must appear at the very beginning of a file or function before any other statements.
If you place it elsewhere, it wonβt work.
Example:
"use strict";
function test() {
// Your code in strict mode
}
You can also enable strict mode for specific functions by placing the directive at the start of the function:
function test() {
"use strict";
// Code within the function runs in strict mode
}
What Does Strict Mode Change in JavaScript? π
Strict mode introduces several changes to JavaScript's behavior to help eliminate hidden bugs and improve code security.
Here are some key changes:
1. Prohibits Using Undeclared Variables
In strict mode, you cannot use variables without declaring them first.
Attempting to assign a value to an undeclared variable results in an error.
Example:
"use strict";
x = 10; // Error: x is not defined
In standard mode, the above code would create a global variable, which can lead to issues in larger projects.
2. Prevents Overwriting Read-Only Properties
Strict mode prevents overwriting values that are marked as read-only.
Example:
"use strict";
const obj = {};
Object.defineProperty(obj, "prop", {
value: 42,
writable: false,
});
obj.prop = 100; // Error: Cannot assign to read-only property
3. Eliminates Silent Errors
In standard mode, some errors are ignored or silently handled.
Strict mode makes such errors explicit by throwing exceptions.
4. Forbids Duplicate Parameter Names
In strict mode, you cannot use duplicate parameter names in functions.
Example:
"use strict";
function sum(a, a) {
// Error: Duplicate parameter name not allowed
return a + a;
}
5. Blocks Use of Reserved Keywords
Some words are reserved for future versions of JavaScript, such as let, class, and enum.
Strict mode prohibits using these as variable names or parameters.
Why Use 'use strict'? π€
Using strict mode helps:
- Catch hidden errors β thanks to stricter rules.
- Improve code security β prevents accidental creation of global variables.
- Enhance performance β some JavaScript engines can optimize strict mode code.
- Future-proof your code β by avoiding reserved keywords.
When Is 'use strict' Not Enabled by Default? β οΈ
Strict mode is not enabled by default in JavaScript to maintain compatibility with older code.
This means you must explicitly include the "use strict"
directive at the beginning of your file or function to use it.
However, in modern JavaScript specifications, such as ECMAScript 6 (ES6) and later, modules and classes automatically run in strict mode.
Does 'use strict' Apply to All Code? π
If you place the "use strict"
directive at the top of a file, all code in that file will execute in strict mode. However, if you place it inside a function, only the code within that function will be affected.
Example:
function oldFunction() {
// Code in standard mode
}
function newFunction() {
"use strict";
// Code in strict mode
}
Exceptions and Errors in 'use strict' π¨
In strict mode, some previously ignored errors now throw exceptions. For example:
-
Attempting to delete non-deletable properties:
javascript"use strict"; delete Object.prototype; // Error
-
Attempting to overwrite
NaN
,Infinity
, orundefined
:javascript"use strict"; NaN = 123; // Error
Conclusion π
"use strict"
in JavaScript is a directive that enables strict mode, which changes how the JavaScript engine interprets code.
Strict mode helps eliminate silent errors, improves code security, and prepares code for future versions of the language.
Key Takeaways:
- To enable strict mode, include
"use strict";
at the start of your file or function. - It prevents undeclared variables and other potential issues.
- It is not enabled by default for backward compatibility.
JavaScript is a language that continually evolves. Using "use strict"
is a step toward writing more robust and secure code.
Good luck ππ¨βπ»