“This is the 11th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
Use Script is a development pattern commonly used in JavaScript programming. We call it the strict pattern.
The main feature of this pattern is that it allows developers to use only a limited syntax and avoid unnecessary errors.
In a junior developer’s project, it doesn’t cause much of a problem. But if the project is large, “small” errors in the code can cause big problems. To prevent this from happening, the developers applied the Use Strict mode during JavaScript development.
Strict mode Forces the system to stop program execution if an error is detected. In other words, this pattern can be used to write programs without defects.
Use strict mode in JavaScript
Using Strict in JavaScript can be applied to an entire document or to a single document function at once. To enable this pattern in your document, place the expression “Use strict” in the correct position with either single or double quotes. Example:
// Strict mode syntax is enabled throughout the script
"use strict";
var v = "Hi! I'm a strict mode script!";
Copy the code
If you want to Use strict mode in a separate function, just type “Use strict” at the start of it. With this, all parts of the function will be in “strict mode,” including nested functions. Example:
function strict() {
// Function level strict schema syntax
'use strict';
function nested() {
return "And so am I!";
}
return "Hi! I'm a strict mode function! " + nested();
}
function notStrict() {
return "I'm not strict.";
}
Copy the code
The difference between strict mode and standard mode in JavaScript
Using Use Strict makes the following changes to JavaScript:
-
In strict mode, undefined variables are not assigned.
-
You cannot use the “with” statement.
-
Duplicate attributes cannot be added to object text.
-
Additional formal function parameters cannot be added.
-
When the Parameter object changes, the parameter does not change.
-
Delete raises an error when the argument is an immutable property of an object.
-
There is no way to convert “this” to an object.
-
You cannot change “eval” and “arguments” and use them as names.
-
Increased the number of words reserved for future use.
-
There is no option to use the octal system.
-
There is no way to apply code structures that might be difficult to optimize the code itself.
-
You cannot declare variables in code passed to the “eval” method.
-
Regular variables cannot be deleted.
-
.
conclusion
Strict mode makes it easier to write “safe” JavaScript code.
The major browsers now have strict mode. But don’t blindly rely on it, because there are still plenty of browser versions on the market that only partially support strict mode or don’t support it at all (such as pre-IE10 versions). Relying on these changes can cause problems or errors in browsers that do not implement strict patterns.
So you need to be careful to use strict patterns and ensure that strict patterns don’t break by checking the functionality of the relevant code.
It’s often asked: Should I always use ‘use Strict’ mode?
There is no definite answer. On the one hand, this pattern improves the code, but on the other hand, development time increases when coding in this pattern. So it’s up to the developer to decide whether to use strict mode or not.