This was the case until 2009 when ECMAScript 5 (ES5) appeared. It added new features to the language and modified some of the existing ones. To keep the old code working, most modifications are off by default. You need to explicitly enable them with a special directive:
The directive looks like a string:
"use strict" or
'use strict'. When it is located at the top of a script, the whole script works the “modern” way.
"use strict"; // this code works the modern way ...
We will learn functions (a way to group commands) soon.
Looking ahead, let’s just note that
"use strict" can be put at the start of most kinds of functions instead of the whole script. Doing that enables strict mode in that function only. But usually, people use it for the whole script.
Please make sure that
"use strict" is at the top of your scripts, otherwise strict mode may not be enabled.
Strict mode isn’t enabled here:
alert("some code"); // "use strict" below is ignored--it must be at the top "use strict"; // strict mode is not activated
Only comments may appear above
There is no directive like
"no use strict" that reverts the engine to old behavior.
Once we enter strict mode, there’s no return.
We have yet to cover the differences between strict mode and the “default” mode.
In the next chapters, as we learn language features, we’ll note the differences between the strict and default modes. Luckily, there aren’t many and they actually make our lives better.
For now, it’s enough to know about it in general:
"use strict"directive switches the engine to the “modern” mode, changing the behavior of some built-in features. We’ll see the details later in the tutorial.
- Strict mode is enabled by placing
"use strict"at the top of a script or function. Several language features, like “classes” and “modules”, enable strict mode automatically.
- Strict mode is supported by all modern browsers.
- We recommended always starting scripts with
"use strict". All examples in this tutorial assume strict mode unless (very rarely) specified otherwise.