Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Last active August 23, 2023 23:30
Show Gist options
  • Save dfkaye/81bed1bc0a2173ddd5f750dd1212111f to your computer and use it in GitHub Desktop.
Save dfkaye/81bed1bc0a2173ddd5f750dd1212111f to your computer and use it in GitHub Desktop.
Short note about strict mode ("use strict" directive)

19 February 2022

Short note about "use strict"

"Non-simple" parameters

MDN doc about SyntaxError: "use strict" not allowed in function with non-simple parameters.

default parameters

function df(any = "") {
  "use strict";
}
Uncaught SyntaxError: "use strict" not allowed in function with default parameter

destructuring parameters

function ds({ any = "" }) {
  "use strict";
}
// Uncaught SyntaxError: "use strict" not allowed in function with destructuring parameter

rest parameters

function rp(...any) {
  "use strict";
}
// Uncaught SyntaxError: "use strict" not allowed in function with rest parameter

arguments and eval as keys

NodeJS issue filed (then closed) by Andrea Giammarchi (@webreflection), Unable to destructure arguments as generic key #42051.

Error message in Node.js reads, "SyntaxError: Unexpected eval or arguments in strict mode".

In the browser (Firefox) console, things are slightly different.

Example:

function A(arguments) {
  "use strict";
}
// Uncaught SyntaxError: 'arguments' can't be defined or assigned to in strict mode code

However, if arguments is a destructured key, then the destructuring parameters message will appear instead.

function A({ arguments }) {
  "use strict";
}
// Uncaught SyntaxError: "use strict" not allowed in function with destructuring parameter

Even if the function is defined in a nested context, inside another function with the "use strict" directive, the syntax error appears.

var A = (function() {
  "use strict";
  return function A({ arguments }) {
    console.log(arguments);
  }
})()

// Uncaught SyntaxError: 'arguments' can't be defined or assigned to in strict mode code

What if it's a field on an object passed in?

function A(o) {
  "use strict";
  console.log(Object(o).arguments);
}

A({ arguments: 9 });
// 9

ES6 classes and modules are strict by default

The browser console is not strict by default, but ES6 classes (and modules) are, so we can use classes to demonstrate all the examples.

As a parameter name

class A {
  noop(arguments) { }
}

// Uncaught SyntaxError: 'arguments' can't be defined or assigned to in strict mode code

Destructured parameter name

class A {
  noop({ arguments }) { }
}

// Uncaught SyntaxError: 'arguments' can't be defined or assigned to in strict mode code

Assignment. As in var arguments.

class  A{
  noop(any) {
    var arguments = 1;
    console.log(arguments)
  }
}

// Uncaught SyntaxError: 'arguments' can't be defined or assigned to in strict mode code

Surprisingly, rest parameters pass, even though "classes are strict by default".

class A {
  noop(...rest) { }
}

And so does the usual arguments reference.

class A {
  noop(...rest) { console.log(arguments) }
}

new A().noop(1,2,3)

// Arguments { 0: 1, 1: 2, 2: 3, … }

Thus arguments may still be referenced inside a class method as long as is not defined or assigned to.

That seems inconsistent.

more examples using classes needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment