Modern mode, “Use strict”

JavaScript has evolved over time without causing any compatibility problems. New features are added and old features remain unchanged.

This is good for compatibility with older code, but the downside is that any mistakes or imperfect decisions made by the JavaScript creator will also remain in the JavaScript language forever.

This continued until the advent of ECMAScript 5 (ES5) in 2009. The ES5 specification adds new language features and modifies some existing features. Most of the changes are disabled by default in order to ensure that the old functionality is still available. You need a special directive — “Use strict” — to explicitly activate these features.

“use strict”

This directive looks like a string “use strict” or ‘use strict’. When it is at the top of the script file, the entire script file works in “modern” mode.

Such as:

"use strict";

// The code works in modern mode.Copy the code

We’ll look at functions (a way of combining commands) later.

However, it should be noted in advance that “use strict” can be placed at the beginning of the function body, not the beginning of the entire script. This allows strict mode to be enabled only in this function. But often people will enable strict mode throughout the script.

Make sure “use strict” appears at the top

Make sure “Use strict” appears at the top of the script, otherwise strict mode may not be enabled.

Here strict mode is not enabled:

alert("some code");
// The following "use strict" is ignored and must be at the top.

"use strict";

// Strict mode is not activated
Copy the code

Only comments can appear on top of “Use strict”.

There is no way to canceluse strict

There is no directive like “no use strict” to return the program to default mode.

Once you’re in strict mode, there’s no turning back.

Browser console

In the future, when you use the browser console to test the functionality, please note that Use Strict is not enabled by default.

Sometimes, using Use strict can have a different effect and you’ll get the wrong result.

You can try pressing key:Shift+Enter to type multiple lines of code, and then place use strict at the top, like this:

'use strict'; <Shift+EnterNewline >/ /... Your code<Press the EnterTo run the >
Copy the code

It works in most browsers, like Firefox and Chrome.

If that still doesn’t work, the surest way to make sure use Strict is turned on is to type code into the console like this:

(function() {
  'use strict';

  / /... Your code...}) ()Copy the code

Always use “Use strict”

We haven’t covered the difference between using “Use Strict” versus “default” mode.

In the following sections, we’ll note the difference between strict mode and default mode when we look at language features. Fortunately, there’s not that much difference. And these differences allow us to program better.

For now, this is generally enough to know:

  1. "use strict"The directive converts the browser engine into “modern” mode, changing the behavior of some of the built-in features. We’ll get to those details later in the course.
  2. Strict mode will be adopted"use strict"Enabled at the top of the entire script or function. New language features such as “classes” and “modules” also automatically enable strict mode.
  3. All modern browsers support strict mode.
  4. We recommend using it all the time"use strict"Start the script. All examples in this tutorial default to strict mode unless specified (very rarely).

This tutorial was first published on the wechat public account “Technology Talk”.


Modern JavaScript Tutorials: Open source modern JavaScript tutorials from beginner to advanced quality. React provides a JavaScript tutorial for MDN learners.

Read for free online: zh.javascript.info


Scan the QR code below and follow the wechat public account “Technology Chat” to subscribe for more exciting content.