DEV Community

Cover image for "use strict" in JavaScript and its Benefits
Lawaanu Borthakur
Lawaanu Borthakur

Posted on • Updated on

"use strict" in JavaScript and its Benefits

As we know that JavaScript is known for it's weird behaviors. There are certain situation in JavaScript, which we know that we should not do but we can do and we will not get any errors for it. So, in order solve this problem "use strict" was introduced. Let me explain you how it solves the problem.

"use strict"

"use strict" is a directive that came out with ES5 which helps to write more secure code with less prone errors.

what changes "use strict" made?

It changes the way that the JavaScript is executed. It helps to use a strict operating context, what that really means is that the errors which were being ignored by the compiler, will now throw exception messages. Strict mode is supposed to help us write more secure JavaScript code. Basically JavaScript has been really forgiving bad syntax and it lets us get away with things that we shouldn't usually code which could have side-effects like using variables that have yet to be defined. So, let me explain you with some examples:
Example 1:

language="JS";
console.log(language);//Output: JS
Enter fullscreen mode Exit fullscreen mode
"use strict"
language="JS";
console.log(language);//Output: ReferenceError
Enter fullscreen mode Exit fullscreen mode

If we run the above code without "use strict" we will see that we will get the output as JS but with "use strict" it will throw an reference error as we are not explicitly defining the language variable with keywords like var, let, const.So now lets move to an another example.
Example 2:

(function programming()
{
language="JS";
console.log("Inside Function->"+language);//Output:Inside Function->JS
})();
console.log("Outside Function->"+language);//Output:Outside Function->JS
Enter fullscreen mode Exit fullscreen mode
(function programming()
{
"use strict"
language="JS";
console.log("Inside Function->"+language);//Output:Inside Function->JS
})();
console.log("Outside Function->"+language);//Output:ReferenceError
Enter fullscreen mode Exit fullscreen mode

If we run the above code which includes a self-executing function.Without "use strict" , we will notice that language variable behaves as a global variable and can be accessed outside the function. This is probable not what a developer intended to do and it could have unintended side-effects.With "use strict" ,we will notice that outside the function it will throw a reference error as we are not explicitly defining the language variable with any of the keywords like var, let, const.

We can set the entire program to a strict mode or scope it to a specific function.

Let us see more what else a strict mode prevent us from doing.

1.Deleting a variable, a function or an argument is not allowed.

"use strict"
language="JS";
delete language;//This will throw an error
Enter fullscreen mode Exit fullscreen mode

2.Using an object, without declaring it, is not allowed.

"use strict";
 language= {l1:"JS", l2:"JAVA"};      // This will cause an error
Enter fullscreen mode Exit fullscreen mode

3.Deleting a function is not allowed.

"use strict";
function language(l1, l2) {};
delete language;                // This will cause an error 
Enter fullscreen mode Exit fullscreen mode

4.Duplicating a parameter name is not allowed.

"use strict";
function language(l1, l1) {};   // This will cause an error
Enter fullscreen mode Exit fullscreen mode

5.Octal numeric literals are not allowed.

"use strict";
var language = 010;             // This will cause an error
Enter fullscreen mode Exit fullscreen mode

6.Escape characters are not allowed.

"use strict";
var language = \010;            // This will cause an error

Enter fullscreen mode Exit fullscreen mode

7.Writing to a read-only property is not allowed.

"use strict";
var language = {};
Object.defineProperty(language, "l1", {value:0, writable:false});
language.l1 = 3.14;            // This will cause an error
Enter fullscreen mode Exit fullscreen mode

8.Writing to a get-only property is not allowed.

"use strict";
var language = {get l1() {return 0} };
language.l1 = 3.14;            // This will cause an error
Enter fullscreen mode Exit fullscreen mode

9.Deleting an undeletable property is not allowed.

"use strict";
delete Object.prototype; // This will cause an error
Enter fullscreen mode Exit fullscreen mode

10.The string "eval" cannot be used as a variable.

"use strict";
var eval = 3.14;         // This will cause an error
Enter fullscreen mode Exit fullscreen mode

11.The string "arguments" cannot be used as a variable.

"use strict";
var arguments = 3.14;    // This will cause an error

Enter fullscreen mode Exit fullscreen mode

12.The with statement is not allowed.

"use strict";
with (Math){x = cos(2)}; // This will cause an error
Enter fullscreen mode Exit fullscreen mode

13.For security reasons, eval() is not allowed to create variables in the scope from which it was called.

"use strict";
eval ("var x = 2");
alert (x);               // This will cause an error
Enter fullscreen mode Exit fullscreen mode

Should we use "use strict" in our script?

Yes, "use strict" was introduced to allow developers to opt-in into a better version of JavaScript.

P.S:As "use strict" is a literal expression and not a statement. So, some older browsers has backward compatibility issue. At the worst, it will simply be ignored and run normally.

Wrap Up!!

I hope you enjoyed this article. Thank you for reading. Please share it with your network. Don’t forget to leave your comments below.

Top comments (0)