JS: What's Strict Mode and How to Determine Strict Mode?

By Xah Lee. Date: . Last updated: .

What is strict mode?

JavaScript ES5 introduced strict mode. When the mode is on, the language has slightly different behavior. It is invented to fix some bad language design of JavaScript.

4.2.2 The Strict Variant of ECMAScript

The ECMAScript Language recognizes the possibility that some users of the language may wish to restrict their usage of some features available in the language. They might do so in the interests of security, to avoid what they consider to be error-prone features, to get enhanced error checking, or for other reasons of their choosing. In support of this possibility, ECMAScript defines a strict variant of the language. The strict variant of the language excludes some specific syntactic and semantic features of the regular ECMAScript language and modifies the detailed semantics of some features. The strict variant also specifies additional error conditions that must be reported by throwing error exceptions in situations that are not specified as errors by the non-strict form of the language.

The strict variant of ECMAScript is commonly referred to as the strict mode of the language. Strict mode selection and use of the strict mode syntax and semantics of ECMAScript is explicitly made at the level of individual ECMAScript source text units. Because strict mode is selected at the level of a syntactic source text unit, strict mode only imposes restrictions that have local effect within such a source text unit. Strict mode does not restrict or modify any aspect of the ECMAScript semantics that must operate consistently across multiple source text units. A complete ECMAScript program may be composed of both strict mode and non-strict mode ECMAScript source text units. In this case, strict mode only applies when actually executing code that is defined within a strict mode source text unit.

[ECMAScript 2015 §Overview#sec-strict-variant-of-ecmascript]

All major browsers today support strict mode.

ES5 §4#sec-4.2.2

How to turn on strict mode?

To use strict mode, add "use strict" to the top of your source code.

Or add to the top of inside a function. When it's inside a function, strict mode applies to the function's body only.

function myFun () { 
    "use strict"
    // code here

ECMAScript 2015 §ECMAScript Language: Source Code#sec-strict-mode-code

ECMAScript 2015 §ECMAScript Language: Functions and Classes#sec-directive-prologues-and-the-use-strict-directive

ES5 §14#sec-14.1 ES5 §10#sec-10.1.1

How to test if you are in strict mode?

when a function is called as a function in global context, that is, not as a method of a object, then, the value of this keyword inside the function is different depending on whether it's strict mode or not.

// determine if in strict mode

"use strict" // comment this line out to test

function isStrict () { if ( this === undefined ) { return true; } else {return false; } };

Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon