Introduction
“STRICT MODE”!! Did you ever hear about this? You might have seen this written somewhere on StackOverflow, or your friend might have told you that it was asked in his Web Development interview!!
Whatever it might be, I know that it’s a fairly new term for you, so let’s jump straight to the question you have in mind.
Read More About, Basics of Javascript
What is JavaScript Strict Mode?
As the name suggests, Strict Mode enforces some strict principles for a Javascript program; this means that the leverages given by JavaScript are not available while working in the strict mode.
Working in the Strict Mode means that you cannot get away by writing a bad syntax code. JavaScript will avoid simple syntax errors and execute the code flawlessly if you are not working in strict mode.
For instance, if you initialize a variable with some value without declaring the variable in the script, the JavaScript compiler will not throw an error and simply ignore it.
In other words, the machine understands that a human is not a machine, and it makes errors.
Caption: Wholesome robot girl
Source: Reddit
But if you want to make errors and learn from your mistakes like a true leader, then JavaScript Strict Mode is made for you.
JavaScript Strict Mode got introduced in ECMAScript version 5 (2009), along with it many other features like JSON support, String.trim() function, Array.isArray() function, Array iteration methods, etc. were also got added in ECMAScript version 5.
To use JavaScript Strict Mode in your programs, you just have to include one line at the top of your script.
"use strict"; // Rest of the program |
You can use javascript strict mode in two ways- if you declare it at the top of the script, then it will be applied for the entire program, and if you declare this in a function body, then the scope of strict mode will be local meaning it will be applied to only the function.
function demo(){ "use strict"; console.log("JavaScript Strict Mode demo"); } |
Let’s learn from an Example.
index.js
function multiply(a, b){ m = a*b; return m; } console.log(multiply(10, 22)); |
As you can see in my index.js file, I have not declared the ‘m’ variable, which is a syntax fault.
Let’s see what output I get from this.
Caption: Console output without strict mode
In my console, why am I getting the correct result? Yes, you guessed it right!! It’s because the JavaScript compiler ignores these petty issues, so we don’t have to worry about it.
Now let’s see what happens when I use strict mode.
index.js
"use strict"; // working in strict mode function multiply(a, b){ m = a*b; return m; } console.log(multiply(10, 22)); |
Output:
Caption: Console output with strict mode
Now, Why am I getting an error that m is not defined? Yes, Exactly, you guessed it right again!! Since we are working in Strict Mode, JavaScript doesn’t ignore our errors and throws them on the console.
I’ll have to syntactically correct my code now (that is, declare m before using it)
index.js
"use strict"; // working in strict mode function multiply(a, b){ var m = a*b; return m; } console.log(multiply(10, 22)); |
This code will not give any error and prints 220 on the console.
I know you guys must be wondering that if the Strict Mode is so cruel and punishes us for making mistakes, why is it even there, and why do we have to include this in our programs.