JS: Strict Mode
What is strict mode
JavaScript standard in year 2011 introduced strict mode. From Standard ECMA-262, 5.1 Edition / June 2011, ECMAScriptยฎ Language Specification Annex C: The Strict Mode of ECMAScript
It is a piece of code like this:
"use strict"
added to the beginning of JavaScript source code file, or inside function at top.
This line of string is called directive.
When strict mode is on, the language has slightly different behavior. Strict mode is created to fix some worst warts of JavaScript.
All major browsers since 2013 support strict mode.
What does strict mode do
The tech details of what strict mode do is fairly complex, and is irrelevant to average JavaScript programer. Here's some salient items:
- Assignment to an undeclared identifier or otherwise unresolvable reference does not create a property in the global object.
- The this Binding, now when used outside of function, return undefined instead of pointing to browser window object.
- arguments Object now cannot be changed.
- The โdeleteโ Operator now cannot be used on a variable or function.
- Function with duplicate named formal parameters is now error.
- Many words, such as
implements
,interface
,let
,package
,private
,protected
,public
and more, are now reserved words. e.g. You cannot use them as variable names. - Number starting with 0 is no longer considered an octal notation.
here's JavaScript spec on strict mode.
How to turn on strict mode
To use strict mode, add to the beginning of your source code this line:
"use strict"
you can also add it to the top of inside a function. When it's inside a function, strict mode applies to the function's body only.
// strict mode just for this function function myFun() { "use strict"; // code here }
๐ก TIP: i do not recommend adding strict mode to function. It makes things too complex. Just add it to top of all JavaScript files.
Should You Turn on Strict Mode?
Yes, in general. Especially in a big company where lots codes are written by bad coders.
- Strict mode began in 2011.
- New features of JavaScript language since 2015 ใsee 2015 Featuresใ are designed to behave like in strict mode, without needing the strict mode declaration, e.g. JavaScript modules. ใsee JS: Exportใ
- Many JavaScript compilers, auto add strict mode directive, or the language is defined to be like in strict mode, such as TypeScript.
- If you are a careful JavaScript coder, your code likely do not require any fixes done by strict mode. However, its still a good idea to add strict mode directive when you are not lazy.
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.
ใsee this Bindingใ
- in strict mode, global context function call:
this
evaluates toundefined
. - in non strict mode, global context function call:
this
evaluates to the Global Object.
// 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());