liveBook · Manning

Strict mode

As explained in Secrets of the JavaScript Ninja, Second Edition

A JavaScript mode that applies stricter parsing and runtime semantics.

Strict mode, added in ES5, changes JavaScript semantics: engines throw errors instead of silently accepting some behavior, and some unsafe features are banned. One practical effect is that the arguments object no longer aliases matching function parameters. section 4.1.1

Listing 4.4. Using strict mode to avoid arguments aliasing
"use strict";                                   #A    

function infiltrate(person){
  assert(person === 'gardener',                 #B  
    'The person is a gardener');                #B
  assert(arguments[0] === 'gardener',           #B
    'The first argument is a gardener');        #B

  arguments[0] = 'ninja';                       #C

  assert(arguments[0] === 'ninja',              #D
    'The first argument is now a ninja');       #D

  assert(person === 'gardener',                 #E
    'The person is still a gardener');          #E
}

infiltrate("gardener");                             

With the "use strict" directive, changing arguments[0] to "ninja" leaves the gardener parameter unchanged. section 4.1.1

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Defines strict mode as an ES5 behavior change and demonstrates detached arguments, while deferring other changed or banned features.
Examples
Compares parameter and arguments mutations using a gardener parameter and the value "ninja".
What it assumes
Assumes familiarity with function parameters, arguments passed at invocation, and the arguments object.
What you can do after
Apply strict mode with a "use strict" directive to keep arguments-object mutations from changing matching parameters.
How it connects
Connects strict mode to the arguments object, function parameters, and this.

Related concepts

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