Know About "use Strict" In JavaScript
Hello everyone, my name is Aryan Sharma, and I hope you're all doing well.
“use strict” JavaScript has developed without any compatibility problems for a very long period. The language now has additional capabilities while maintaining its previous functionality.
This situation continued up until the release of ECMAScript 5 (ES5) in 2009. It changed some of the previous characteristics and added new ones to the language.
Most of these improvements are disabled by default to preserve the old code functioning. The specific directive "use strict" must be used to expressly enable them.
When "use strict" or 'use strict' is located at the top of a script, the whole script works the “modern” way.
Your JavaScript code here Ensure that “use strict” is at the top.
🚫Please make sure that "use strict" is at the top of your scripts, otherwise strict mode might not be enabled.
There is no such directive like "no use strict" so to undo the effect of use strict
Once we enter strict mode, there’s no going back.
Now, you might be thinking why add use script ? or what are the benefits of adding use script in our JS code?🤔
Why use "use state"? One could recommend starting scripts with "use strict"… But you know why it is important to do so....?
Strict mode variables: In strict mode, variables must be declared before they are used. This prevents the accidental creation of global variables by omitting the var, let, or const keyword.
Eliminating silent errors: In strict mode, certain coding practices that can lead to silent errors are treated as explicit errors. For example, assigning values to read-only properties, deleting variables, or duplicating parameters.
Strict mode for functions: When "use strict" is applied to a function, that function and its nested functions operate in strict mode. If you're using modules, each module is implicitly in strict mode.
Changes to the "this" value: In strict mode, the value this inside a function is not automatically coerced to the global object (e.g., window in browsers) or to undefined in strict mode functions that are not methods. Instead, it remains the value it was set to when the function was called.
Octal literals and escape sequences: Octal literals (e.g., 0123) and certain escape sequences (e.g., \012) are not allowed in strict mode, as they were a source of confusion and potential errors.
A strict mode is a useful tool for catching and preventing common programming mistakes. It encourages cleaner and more reliable JavaScript code by eliminating certain language features that are prone to errors or considered bad practices.
I Hope you loved the article❣️
Don't forget to follow me