It had been so before 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. One needs to enable them explicitly with a special directive
The directive looks like a string:
"use strict" or
'use strict'. When it is located on the top of the script, then the whole script works the “modern” way.
"use strict"; // this code works the modern way ...
There is no directive
"no use strict" or alike, that would return the old behavior.
Once we enter the strict mode, there’s no return.
Please make sure that
"use strict" is on the top of the script, otherwise the strict mode may not be enabled.
There is no strict mode here:
alert("some code"); // "use strict" below is ignored, must be not on the top "use strict"; // strict mode is not activated
Only comments may appear above
use strictfor functions
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 a function (most kinds of functions) instead of the whole script. Then strict mode is enabled in that function only. But usually people use it for the whole script.
It is recommended to always start a script with
"use strict", for the following reasons:
- First, all modern browsers support it. Only outdated ones like Internet Explorer 9 and below do not.
Here in the tutorial all code (where not explicitly noted otherwise) works in
"use strict", so that you can understand what’s going on if you forget it or if you’re working with an outdated script that doesn’t have it.
"use strict"directive switches the engine to the “modern” mode, changing the behavior of some builtin features.
- Several modern features of the language enable
"use strict"implicitly, so it’s quite hard to evade it.
It’s always recommended to start scripts with
"use strict". All examples in this book assume so, unless (very rarely) specified otherwise.