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

JavascriptEcmascript 5Ecma262Strict Mode

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

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 - Javascript

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

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

More elegant way: if "this" is object, convert it to true

"use strict"

var strict = ( function () { return !!!this } ) ()

if ( strict ) {
    console.log ( "strict mode enabled, strict is " + strict )
} else {
    console.log ( "strict mode not defined, strict is " + strict )
}

Solution 7 - Javascript

Another solution can take advantage of the fact that in strict mode, variables declared in eval are not exposed on the outer scope

function isStrict() {
    var x=true;
    eval("var x=false");
    return x;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeepak PatilView Question on Stackoverflow
Solution 1 - JavascriptThiefMasterView Answer on Stackoverflow
Solution 2 - JavascriptnoseratioView Answer on Stackoverflow
Solution 3 - JavascriptThalaivarView Answer on Stackoverflow
Solution 4 - JavascriptMehdi GolchinView Answer on Stackoverflow
Solution 5 - JavascriptpotatoView Answer on Stackoverflow
Solution 6 - Javascriptuser6748331View Answer on Stackoverflow
Solution 7 - JavascriptYaron U.View Answer on Stackoverflow