I recently come across "strict mode" in JavaScript. I know that strict mode is very important to get programmer coding JavaScript in proper way. My question is, does removing "strict mode" when putting the script up online will bring better "stability"? Is it the good practice to do so? Also, does "strict mode" help to improve performance of your web app?
-
Strict mode is for developers, it doesn't do anything for performance or for users. There are many browsers in use that don't support strict mode, so don't depend on it being available. Ensure that code written in strict mode also runs in non–strict mode. – RobG Jan 04 '15 at 09:26
-
Imagine a user does something you never tested for, and in that particular case, you're misusing something that `use strict` could've caught. Plus, there are some obscure cases where non-strict mode acts differently. In either case, `use strict` would be useful to have. Since it doesn't impair performance, I'm not sure what motivation there is to strip it out. – Kevin Mar 06 '17 at 12:29
1 Answers
In theory - running in sloppy mode can cause bugs
Removing strict mode changes the semantics of the language and might introduce subtle bugs to a large codebase developed in strict mode. For example the arguments
object behaves differently in strict mode and so does this
when a method is called on a primitive so code relying on either of those will break.
Some examples of behavior diverging:
(function(){ console.log(typeof this === "object") }).call(5); // logs true
(function(){ "use strict"; console.log(typeof this === "object") }).call(5); // false
(function(x){ arguments[0] = 3; console.log(x) })(5); // logs 3
(function(x){ "use strict"; arguments[0] = 3; console.log(x) })(5); // logs 5
In practice it's not that bad
Most of these edge cases are things you should not run into anyway. Strict mode is beneficial for catching bugs like declaring global variables implicitly and assigning to the global object as this
. If your code runs on IE9 it cannot utilize strict mode anyway and it runs in sloppy mode in IE9- and strict mode on newer browsers. I've personally run multiple such code bases on IE9 in sloppy mode and other browsers in strict mode and I never ran into actual bugs because of it.
On performance
Strict mode removed "dynamic scoping" by making arguments
behave well and prohibiting with so it's supposed to perform faster by removing a whole layer of lookups - that said modern browsers figure this sort of thing on their on so you shouldn't feel a big performance difference. The main advantage of strict mode is that it helps prevent subtle bugs.

- 270,886
- 87
- 504
- 504
-
Thanks a lot for your answer. So does that mean, turning on strict mode, old browser like IE 8, 9 will behave differently and cause trouble? – user1995781 Jan 04 '15 at 08:13
-
1@user1995781 yes - they will behave differently and in theory cause trouble by having different semantics. In practice: When was the last time you assigned to `arguments` or used `.call` on a primitive? The semantics changes are mostly harmless in reasonable code bases although they are definitely there. – Benjamin Gruenbaum Jan 04 '15 at 08:14
-
Also, when was the last time you wrote `if (typeof this == 'undefined')`? Code written in strict mode must run in non–strict mode anyway, so it should be tested that way (so if you're using a good linter, what's the point of strict mode at all?). – RobG Jan 04 '15 at 09:32