How to check if JavaScript code is running in strict mode?
30 December 2022 (Updated 31 December 2022)
Declare a helper function at the global level:
function isStrictMode() {
return typeof this === 'undefined'
}
function isSloppyMode() {
return this instanceof Window
}
If JavaScript code is running in strict mode, this
will always be undefined if unspecified.
Now, you can do something like:
if (isStrictMode()) {
console.log('Running in strict mode')
}
if (isSloppyMode()) {
console.log('Running in sloppy mode')
}
Tagged:
JavaScript
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment