liveBook · Manning

Strict mode

As explained in Secrets of the JavaScript Ninja, Third Edition

A JavaScript execution mode that applies stricter syntax and runtime semantics.

Strict mode applies stricter JavaScript semantics. For a non-arrow function called on its own, it makes `this` undefined rather than the global object, so mistaken property access fails clearly. A bound function keeps its explicitly bound context. section 6.2.1

A standalone call

In `ninja()`, a non-arrow function runs as a standalone function; in strict mode, its `this` value is undefined. section 6.2.1

Strict mode dates to ECMAScript 5. ECMAScript modules run in strict mode automatically; TypeScript and popular transpilers can also emit the `"use strict"` directive. section 6.2.1 section 2.1.1

function ninja(){};        #A
ninja();                   #A
 
const samurai = function(){};    #B
samurai();                     #B
(function(){})()   #C

Compare several syntactic forms of standalone function invocation. section 6.2.1

"use strict";
setTimeout(200, () => {
    console.log("This will never run.");
});

See the `use strict` directive in TypeScript compiler output. section 2.1.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains the standalone-call `this` behavior and its rationale but defers strict mode's other details.
Examples
Uses standalone calls and TypeScript compiler output containing `use strict` to illustrate the behavior and directive.
What it assumes
Distinguishes standalone calls from method calls while explaining how invocation affects `this`.
What you can do after
Understand how strict mode affects `this` in standalone calls and when binding supplies a different context.
How it connects
Connects strict mode to function invocation, `this`, `bind`, ECMAScript modules, TypeScript, and Babel.

Also explained in

Compare how 2 books explain strict mode →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.