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

, , …,

What's 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.

All major browsers today support strict mode.

ECMAScript §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. (you can also add it inside a function. When it's inside a function, strict mode applies to the function's body only.)

ECMAScript §14#sec-14.1 ECMAScript §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; } };

console.log(isStrict());
blog comments powered by Disqus