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
 
    
    - 50,140
- 28
- 121
- 140
 
    
    - 3,419
- 3
- 25
- 22
- 
                    You could use `(() => !this)()`, e.g., `(() => !this)() ? "strict" : "sloppy"` – Eljay Jun 23 '23 at 12:31
7 Answers
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
 
    
    - 310,957
- 84
- 592
- 636
- 
                    11For 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. – aljgom Mar 15 '17 at 21:40
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.
 
    
    - 59,932
- 34
- 208
- 486
- 
                    Just out of curiosity, how bulletproof is this in 2015, now that ES6 is here? – John Weisz Jul 18 '15 at 17:02
- 
                    3I verify that it works in ES6 on latest chrome and nodejs. – Michael Matthew Toomim Nov 30 '16 at 00:27
- 
                    2
- 
                    Doesn't it affect the performance of an application, especially React one? – ellockie Jul 12 '23 at 18:05
- 
                    1@ellockie I wouldn't do it on a tight rendering loop, but as one-off per module init, I don't think so. Anyhow, there might be better ways of doing this in 2023. – noseratio Jul 13 '23 at 01:23
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
 
    
    - 5,179
- 10
- 35
- 56
 
    
    - 23,282
- 5
- 60
- 71
- 
                    1This is better than Mehdi's answer as it will work everywhere, not only in a global scope. Upped. :) – mgol Aug 15 '12 at 23:45
- 
                    7This results in a syntax error, which happens before the code runs, so it can't be caught... – Jelle De Loecker Jun 28 '13 at 12:11
- 
                    7This will not work in ES6 either as the check is removed to allow computed property names. – billc.cn Jan 30 '15 at 10:22
- 
                    1
- 
                    1@skerit Can you elaborate on your syntax error? I do not get one. – Robert Siemer Feb 25 '20 at 00:48
- 
                    The spec and engines behavior have changed. Now this throws in old engines, but it does not throw in new engines. https://stackoverflow.com/questions/30617139/whats-the-purpose-of-allowing-duplicate-property-names – shitpoet Jul 29 '21 at 11:46
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.
 
    
    - 995
- 11
- 19
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 )
}
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;
}
 
    
    - 7,681
- 3
- 31
- 45
