Is there any way to check if strict mode is enforced?

18,846

Solution 1

The fact that this inside a function called in the global context will not point to the global object can be used to detect strict mode:

var isStrict = (function() { return !this; })();

Demo:

> echo '"use strict"; var isStrict = (function() { return !this; })(); console.log(isStrict);' | node
true
> echo 'var isStrict = (function() { return !this; })(); console.log(isStrict);' | node
false

Solution 2

I prefer something that doesn't use exceptions and works in any context, not only global one:

var mode = (eval("var __temp = null"), (typeof __temp === "undefined")) ? 
    "strict": 
    "non-strict";

It uses the fact the in strict mode eval doesn't introduce a new variable into the outer context.

Solution 3

function isStrictMode() {
    try{var o={p:1,p:2};}catch(E){return true;}
    return false;
}

Looks like you already got an answer. But I already wrote some code. So here

Solution 4

Yep, this is 'undefined' within a global method when you are in strict mode.

function isStrictMode() {
    return (typeof this == 'undefined');
}

Solution 5

Warning + universal solution

Many answers here declare a function to check for strict mode, but such a function will tell you nothing about the scope it was called from, only the scope in which it was declared!

function isStrict() { return !this; };

function test(){
  'use strict';
  console.log(isStrict()); // false
}

Same with cross-script-tag calls.

So whenever you need to check for strict mode, you need to write the entire check in that scope:

var isStrict = true;
eval("var isStrict = false");

Unlike the most upvoted answer, this check by Yaron works not only in the global scope.

Share:
18,846

Related videos on Youtube

Deepak Patil
Author by

Deepak Patil

Updated on July 05, 2020

Comments

  • Deepak Patil
    Deepak Patil almost 4 years

    Is there anyway to check if strict mode 'use strict' is enforced , and we want to execute different code for strict mode and other code for non-strict mode. Looking for function like isStrictMode();//boolean

  • mgol
    mgol almost 12 years
    This is better than Mehdi's answer as it will work everywhere, not only in a global scope. Upped. :)
  • Jelle De Loecker
    Jelle De Loecker almost 11 years
    This results in a syntax error, which happens before the code runs, so it can't be caught...
  • billc.cn
    billc.cn over 9 years
    This will not work in ES6 either as the check is removed to allow computed property names.
  • John Weisz
    John Weisz almost 9 years
    Just out of curiosity, how bulletproof is this in 2015, now that ES6 is here?
  • Buksy
    Buksy almost 8 years
    Why should there be an error thrown in strict mode?
  • Michael Matthew Toomim
    Michael Matthew Toomim over 7 years
    I verify that it works in ES6 on latest chrome and nodejs.
  • aljgom
    aljgom over 7 years
    For clarification, the return statement is equivalent to return this === undefined, it's not comparing it to the global object, it's just checking if this exists.
  • igor
    igor over 5 years
    Nice! Works in NodeJS 10 REPL with/without --use_strict flag.
  • Robert Siemer
    Robert Siemer over 4 years
    @skerit Can you elaborate on your syntax error? I do not get one.
  • shitpoet
    shitpoet almost 3 years
    The spec and engines behavior have changed. Now this throws in old engines, but it does not throw in new engines. stackoverflow.com/questions/30617139/…