"use strict" in JavaScript and its Benefits
Tags: javascript, webdev, programming, tutorial
Published: 2022-01-11 12:48:43

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:**

```javascript
language="JS";
console.log(language);//Output: JS
```

```javascript
"use strict"
language="JS";
console.log(language);//Output: ReferenceError
```
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:**

```javascript
(function programming()
{
language="JS";
console.log("Inside Function->"+language);//Output:Inside Function->JS
})();
console.log("Outside Function->"+language);//Output:Outside Function->JS
```

```javascript
(function programming()
{
"use strict"
language="JS";
console.log("Inside Function->"+language);//Output:Inside Function->JS
})();
console.log("Outside Function->"+language);//Output:ReferenceError
```
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.

```javascript
"use strict"
language="JS";
delete language;//This will throw an error
```
2.Using an object, without declaring it, is not allowed.

```javascript
"use strict";
 language= {l1:"JS", l2:"JAVA"};      // This will cause an error
```
3.Deleting a function is not allowed.

```javascript
"use strict";
function language(l1, l2) {};
delete language;                // This will cause an error 
```

4.Duplicating a parameter name is not allowed.

```javascript
"use strict";
function language(l1, l1) {};   // This will cause an error
```

5.Octal numeric literals are not allowed.

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

6.Escape characters are not allowed.

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

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

```javascript
"use strict";
var language = {};
Object.defineProperty(language, "l1", {value:0, writable:false});
language.l1 = 3.14;            // This will cause an error
```
8.Writing to a get-only property is not allowed.

```javascript
"use strict";
var language = {get l1() {return 0} };
language.l1 = 3.14;            // This will cause an error
```
9.Deleting an undeletable property is not allowed.

```javascript
"use strict";
delete Object.prototype; // This will cause an error
```

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

```javascript
"use strict";
var eval = 3.14;         // This will cause an error
```
11.The string "arguments" cannot be used as a variable.

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

```
12.The with statement is not allowed.

```javascript
"use strict";
with (Math){x = cos(2)}; // This will cause an error
```
13.For security reasons, eval() is not allowed to create variables in the scope from which it was called.
```javascript
"use strict";
eval ("var x = 2");
alert (x);               // This will cause an error
```

## 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.