DEV Community

Alex
Alex

Posted on

"use strict" in JavaScript

The "use strict" in your JavaScript code turns on strict mode. (Or does nothing, in pre-fifth ECMAScript implementations). This directive applies to the scope in which it is specified.
Examples of using

// file: ok.js
"use strict"; // affects the whole file
alert ("ok");
(function () {
"use strict"; // valid only within the function
alert ("ok");
} ());

What “use strict” affects?
Below is a list of the most important features of the mode that will save you hours of development.
Prevents duplicate keys in an object
This saves in case of typos and accidental errors.

The code:

(function () {
"use strict";
var x = {
a: 1,
b: 2,
a: 3
}
} ());

Will result in an error

Uncaught SyntaxError: Duplicate data property in object literal not allowed in strict mode

Requires always explicit declaration of variables via var

(function () {
"use strict";
var a = 1; // correctly
x = 2; // error: no var
} ());

Uncaught ReferenceError: x is not defined

This is a great opportunity! It saves you from mistakes that are very difficult to find. When you have two completely independent functions tied through a variable that suddenly turned out to be global.

But you need to be careful with this feature, it works well only if the entire script is in strict mode. In the following example, strict mode is powerless:

// file: ok.js
// at the file level strict mode is not enabled
x = 0; // global variable (or rather, in this case, the window property)
(function () {
"use strict";
x = 2; // var is not present, but no error will occur, since the global x exists
} ());

This can lead to unpleasant consequences. Such a simple and natural code will lead to a fatal looping and falling into a stupor of the browser.

```function f () {
"use strict";
for (i = 0; i <3; ++ i) {
console.log (i);
}
}

for (i = 0; i <10; ++ i) {
console.log (i);
f ();
}```

The fact is that a call to the f() function will each time set the value of the global variable i (and it turned out to be global here) to 3. And the loop to 10 will never end.

Unfortunately, Strict mode will not help here. But if "use strict" were included globally for the entire file, the browser would immediately inform you about the suspicious code.

Uncaught ReferenceError: i is not defined

Duplicate arguments are not allowed

Typos are not allowed such as:

function sum (a, a) {...

Uncaught SyntaxError: Strict mode function may not have duplicate parameter names

This design can only occur as a result of a typographical error. There is no sense in it, but such code behaves not well. Calls and result inside a function:

sum (); // a === undefined
sum (1); // a === undefined
sum (1, 2); // a === 2

This is most likely not what the developer expects.

The arguments are frozen
You can lose backward compatibility here. The arguments array in strict mode begins to behave like regular arrays (although it does not become a regular array from this). The result is easier to show with an example:

```function a (x) {
arguments [0] = 2;
console.log (x); // prints 2
}

function b (x) {
"use strict";
arguments [0] = 2;
console.log (x); // will print 1
}

a (1);
b (1);```

That is, in normal mode, changing arguments will change the corresponding variables, but in strict mode this does not happen.

This is one of the pitfalls: you will not receive errors and warnings, and the code will work differently

Full note is available in my tech blog

Top comments (0)