
Are you confused or willing to no more about what calls Strict Mode? Here are all what you need to know about it.
Continue after the short break
ad: GET $10,000+ Free Elon Musk Money on X (Twitter) Click Here
ad: Monetize your Facebook Account for Monthly $500 Click Here
ad: How to Earn Free $100 on Binance & Withdraw Click Here
Check Also:
- Jumia Black Friday Sales 90% OFF
- Download All Latest Movies & TV Series
- Download PPSSPP Games
- Download All Premium Apps for Android
- Latest Phones Specs & Prices
- Latest PC Specs & Prices
- Check Latest Tech News & Telcom gist
- How to tech and guides DIY
JavaScript is a forgiving language. Some of its syntax is optional, and the language recovers from errors with more grace than many others. But this hand-holding comes with a penalty: it can be easier to introduce bugs, and inconsistent code is harder to read.
Fortunately, if you want to exercise more discipline, there’s an easy way of doing so: strict mode. Strict mode is a way of asking JavaScript to react more decisively when it encounters problems in your code.
What Is a Strict Mode?
Several languages use the concept of a strict mode: a mode that evaluates and runs code more rigorously. You might be familiar with the HTML strict doctype, which deprecates certain elements and attributes.
Perl, another interpreted scripting language, has long had its own strict mode. This mode forbids certain types of unsafe expression.
How Do I Use Strict Mode in JavaScript?
Inside a script, put a “use strict” statement right at the top, before any other statements:
// this entire script will be in strict mode
'use strict';
Note that you can include a comment before it, but no statements. You can enable strict mode in a JavaScript file, or at the beginning of a script block in an HTML file. You can also enable strict mode on a function-by-function basis:
function strict() {
// Function-level strict mode syntax
'use strict';
return "This function is strict";
}
function loose() {
return "This function is NOT strict";
}
Once you’ve enabled strict mode, make sure you test your code. If you’re working with the web, open a JavaScript console, so you can identify any new errors.
What Does JavaScript’s Strict Mode Do?
In short, the strict mode will be less forgiving of certain types of problematic code. Rather than ignoring issues and continuing execution, certain errors will halt the script. This is often safer than carrying on in undesirable circumstances.
Prevents Accidental Globals
The best example that strict mode protects against is the creation of accidental global variables. In normal execution, this code:
myVar = 17;
It will create a property named myVar on the global object, assuming you haven’t previously declared myVar. In a web browser, the global object is usually window:
console.log(window.myVar);
>> 17
If you include a “use strict” statement, however, you’ll see an error in the console, something like:
Uncaught ReferenceError: myVar is not defined
The reason this is so useful is that it picks up a common case of typo. It’s easy to mistype a variable name, and many languages would pick us up on such an error.
But JavaScript, by default, simply assumes the scope of the global object and continues as if nothing is wrong. Some code may intentionally depend on that behavior, which is something you should be aware of when deciding to use strict mode.
Makes Failure Explicit
Some behavior in JavaScript fails, but it does so silently. You might not know about such errors unless you’re specifically checking for them. For example, NaN is a special property of the global object that represents an invalid number. This property is read-only, but you can still try to write to it:
NaN = 2;
>> 2
But even though it looks as if that assignment succeeded, it didn’t:
NaN
>> NaN
In strict mode, you’ll get an actual error telling you that you can’t assign to NaN. This code uses a function so that you can demo strict mode in the console:
javascript
function badNaN() { "use strict"; window.NaN = 2; }
>> undefined
badNan()
>> Uncaught TypeError: Cannot assign to read only property 'NaN' of object '#'
at badNaN (:1:46)
at :1:1
This is a classic example that shows, while ignorance may be bliss, it’s sometimes better to know if something goes wrong.
Warns About Duplicate Parameters
The final example deals with a little-known feature of JavaScript. It may surprise you to learn that parameter names don’t have to be unique:
function dupeParam(a, a, c) { console.log(a); }
>> undefined
dupeParam(2, 4, 8)
>> 4
Note that JavaScript assigns the latest value to a duplicate parameter. Now, this behavior is not particularly useful. In fact, it would be more useful for JavaScript to tell us it’s an error, and that’s exactly what strict mode does:
function dupeParam(a, a, c) { "use strict"; }
<< Uncaught SyntaxError: Duplicate parameter name not allowed in this context
Use Strict Mode for Extra Code Confidence
Good practices and means of enforcing them go hand-in-hand. In some contexts, such as a professional programming role, you’ll want to exercise as much discipline as possible. Even if you’re just working on a hobby open-source project, the maintainer may prefer to use strict mode as standard.
Ultimately, it’s up to you, but it’s useful to know a helping hand is available. As a programmer, you should always be on the lookout for best practices and what you can do to enforce them.
Advertising statement: The external jump links (including but not limited to hyperlinks, QR codes, passwords, etc.) contained in the article are used to convey more information and save selection time. The results are for reference only. All Naijatechnews articles include this statement.