What Does Strict Do in JavaScript

Web developers often wonder what does strict keyword do in JavaScript. We come across this keyword at the beginning of JavaScript file, or a function. Although it looks very simple and casual, it is a very important command and must be used very carefully. If you do not completely know how strict keyword works, adding it to your code may cause it to stop working properly. In this article, we will learn what is strict keyword, how to use it and what it does.

What is Strict Keyword

The strict keyword was introduced in ECMAScript 5 to enforce certain amount of rigidity in coding practices. Generally, JavaScript is quite flexible and in most cases, it automatically interprets your code correctly even if you do not strictly adhere to the syntax and coding practices. But over time, this can cause many bugs to creep into your code. Also, in some cases of critical codes and modules, you may want the code to be written in a particular manner to avoid ambiguity.

In all such cases, you can use strict keyword. When you add this to your script or function, it will force the web browser to execute the code in strict mode. It enforces a set of rules on your code, and if any of them is not adhered to, forces the web browser to throw an error.

How to Use Strict Keyword

You can simply use strict keyword in your code by adding ‘use strict’ at the beginning of your script or function. If you add it to the beginning of your script, then it has a global scope and will execute all the following code in the script in strict mode. Here is an example to illustrate it.

"use strict";
a = 123; // will give error

In the above code, we add ‘use strict’ at the beginning of page. It will execute the next line of code in strict mode an give error since the variable a has been assigned without declaring.

You can also add it to the beginning of the function.

function abc(){
"use strict";
a = 123;
}

In the above code, we add ‘use strict’ at the beginning of the function. In this case, only when you call the function, you will get an error since its local variable is used before declaration. However, rest of the code in the script remains unaffected.

What Does Strict Do in JavaScript

Using strict keyword in your code will cause many things to happen. So you need to be really careful before you use it. Let us look at some of the important changes. Please note, this is not an exhaustive list.

1. Assignment Without Declaration

Using variables and objects without declaration is generally allowed in JavaScript but not allowed in Strict mode. The following codes will throw an error, since in each case, we are using variable/object without declaring them.

"use strict"
a = 123; // throws error

data = {'b':1,'c':3}; // throws error

On a similar note, assignments that used to fail silently earlier, will throw an error in strict mode. Here is an example that would fail silently in normal mode, but gives an error in strict mode.

"use strict";
NaN = 5;

2. Deleting Variable or function

Also, once you declare a variable, you cannot delete it. If you try to delete it, then your web browser will throw an error. Here’s an example.

"use strict";
var a = 123;
delete a; // throws error

Similarly, you cannot delete a function in strict mode. Otherwise, it too will give an error.

"use strict";
function abc(p1){};
delete abc; // throws error

3. No duplicate parameters

By default, if you accidentally define duplicate parameters in a function definition, JavaScript will continue execution. However, in strict mode, all function parameters must be unique. If there is any duplicate parameter, or a parameter name is repeated in function definition, then you will get an error.

"use strict";
function a(b, b){}; // gives error

Similarly, you cannot use duplicate property names in object literals. But the good part is that this is applicable on old browsers and not ones that support ECMAScript 6.

"use strict";
var data = {'a':1, 'a':2,'b':3}; // gives error

4. Octal Literals and Escape Characters

Generally, the following code will be executed without any error. The web browser will interpret the number as an octal numerical and parse it.

a = 023;
console.log(a); // output is 19

But the above code will give an error when run in strict mode.

"use strict";
a = 023;
console.log(a); // gives error

Similarly, using octal escape characters such as ‘\0’ are also not allowed. Here is an example to demonstrate it.

"use strict";
a = '\023';

In strict mode, octal numeric literals as well as octal escape characters are not allowed.

5. Writing to Read-only Property

In strict mode, you cannot write to read-only property, because by definition they are not editable. Here is an example where define an object data and set its property x to be read only. Then if we try to write to it in strict mode, we will get an error.

"use strict";
const data = {};
Object.defineProperty(data, "x", {value:0, writable:false});

data.x = 123; // gives error

Please note, the above code will run successfully without strict mode.

6. Use of eval

Eval() function evaluates an expression in JavaScript. However, for security reasons, there are many restrictions placed on its functionality, in strict mode. For example, you cannot use a variable name called eval in strict mode because it is a keyword.

"use strict";
let eval = 1; // gives error

Also, eval() function is not allowed to create variables from where it is called.

"use strict";
eval ("x = 1");
alert (x); // gives error

The above example also gives an error because in strict mode, a variable cannot be used before it is defined.

In fact, in eval() function, you are not even allowed to declare a variable using let or var keywords.

"use strict";
eval ("var x = 1");
alert(x); // throws error

The following code will also give error.

"use strict";
eval ("var x = 1");
alert(x); // throws error

7. Keywords as variable names

Similarly, there are many other keywords that you cannot use as variable names, in strict mode. They are arguments, implements, interface, let, package, private, protected, public, static, yield.

"use strict";
let public = 123; // gives error

Conclusion

In this article, we have learnt what strict keyword does in JavaScript. As you can see, it is very useful for enforcing coding discipline, especially in multi-developer environments. But use it only if you have to. Since strict keyword throws an exception in most cases, instead of letting go with a warning, it can break your code, if not used carefully. At this point, it does too many things and if you do not know them all, it can easily throw error. If you are adding it to an existing JavaScript file, make sure you also use plenty of unit tests to ensure that your code does not break. Nevertheless, it is very useful if you want to make sure that people don’t loosely add faulty code to your application or website.

Also read:

How to Check if Array Includes Value in JavaScript
How to Move Element Into Another Element in jQuery
How to Get Selected Text from Dropdown List in jQuery

Leave a Reply

Your email address will not be published. Required fields are marked *