Note: Sometimes you'll see the default, non-strict mode referred to as
sloppy mode. This isn't an official term, but be aware of it, just in case. JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode". Strict mode isn't just a subset: it intentionally has different semantics
from normal code. Browsers not supporting strict mode will run strict mode code with different behavior from browsers that do, so don't rely on strict mode without feature-testing for support for the relevant aspects of strict mode. Strict mode code and non-strict mode code can coexist, so scripts can opt into strict mode incrementally. Strict mode makes several changes to normal JavaScript semantics: See transitioning to strict mode, if you want to change your code to work in
the restricted variant of JavaScript. Strict mode applies to entire scripts or to individual functions. It doesn't apply to block statements enclosed in To invoke strict mode for an entire script, put the exact statement Strict mode for functionsLikewise, to invoke strict mode for a function, put the exact statement
In strict mode, starting with ES2015, functions inside blocks are scoped to that block. Prior to ES2015, block-level functions were forbidden in strict mode. Strict mode for modulesECMAScript 2015 introduced JavaScript modules and therefore a 3rd way to enter strict mode. The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
Strict mode for classesAll parts of ECMAScript classes are strict mode code, including both class declarations and class expressions — and so also including all parts of class bodies.
Changes in strict modeStrict mode changes both syntax and runtime behavior. Changes generally fall into these categories: changes converting mistakes into errors (as syntax errors or at runtime), changes simplifying how the particular variable for a given use of a name is computed, changes simplifying Converting mistakes into errorsStrict mode changes some previously-accepted mistakes into errors. JavaScript was designed to be easy for novice developers, and sometimes it gives operations which should be errors non-error semantics. Sometimes this fixes the immediate problem, but sometimes this creates worse problems in the future. Strict mode treats these mistakes as errors so that they're discovered and promptly fixed. First, strict mode makes it impossible to accidentally create global variables. In normal JavaScript mistyping a variable in an assignment creates a new property on the global object and continues to "work" (although future failure is possible: likely, in modern JavaScript). Assignments, which would accidentally create global variables, instead throw an error in strict mode:
Second, strict mode makes assignments which would otherwise silently fail to throw an exception. For example,
Third, attempts to delete undeletable properties throw in strict mode (where before the attempt would have no effect):
Fourth, strict mode requires that function parameter names be unique. In normal code the last duplicated argument hides previous identically-named arguments. Those previous arguments remain available through
Fifth, strict mode forbids a Novice developers sometimes believe a leading zero prefix has no semantic meaning, so they might use it as an alignment device — but this changes the number's meaning! A leading zero syntax for the octal is rarely useful and can be mistakenly used, so strict mode makes it a syntax error:
Sixth, strict mode forbids setting properties on primitive values. Without strict mode, setting properties is ignored (no-op), with strict mode, however, a
Duplicate property names used to be
considered a
Note: Making code that used to error become non-errors is always considered backwards-compatible. This is a good part of the language being strict about throwing errors: it leaves room for future semantic changes. Simplifying variable usesStrict mode simplifies how variable names map to particular variable definitions in the code. Many compiler optimizations rely on the ability to say that variable X is stored in that location: this is critical to fully optimizing JavaScript code. JavaScript sometimes makes this basic mapping of name to variable definition in the code impossible to perform until runtime. Strict mode removes most cases where this happens, so the compiler can better optimize strict mode code. First,
strict mode prohibits
The simple alternative of assigning the object to a short name variable, then accessing the corresponding property on that variable, stands ready to replace Second,
If the function
Thus names in strict mode Third, strict mode forbids deleting plain names.
Making eval and arguments simplerStrict mode makes First, the names
Second, strict mode code doesn't alias properties of
Third,
"Securing" JavaScriptStrict mode makes it easier to write "secure" JavaScript. Some websites now provide ways for users to write JavaScript which will be run by the website on behalf of other users. JavaScript in browsers can access the user's private information, so such JavaScript must be partially transformed before it is run, to censor access to forbidden functionality. JavaScript's flexibility makes it effectively impossible to do this without many runtime checks. Certain language functions are so pervasive that performing runtime checks has a considerable performance cost. A few strict mode tweaks, plus requiring that user-submitted JavaScript be strict mode code and that it be invoked in a certain manner, substantially reduce the need for those runtime checks. First, the value passed as
Second, in strict mode it's no longer possible to "walk" the JavaScript stack via commonly-implemented extensions to ECMAScript. In normal code with
these extensions, when a function
Strict mode in browsersThe major browsers have fully implemented strict mode since approximately 2012, including IE since version 10, Firefox since version 4. Chrome since version 13, etc. If you still support very old JS environments prior to the roll-outs of strict mode support, be careful to test any of your code that declares strict mode code to verify that its expected behaviors aren't violated when running in non-strict mode conforming JS engines. There are however some nuances to consider with how strict mode behaves in browsers. Strict mode prohibits function statements that are not at the top level of a script or function. In normal mode in browsers, function statements are permitted "everywhere". This is not part of ES5 (or even ES3)! It's an extension with incompatible semantics in different browsers. Note that function statements outside top level are permitted in ES2015. For example, these block-level function declarations should be disallowed in strict mode by the specification's text proper:
However, Appendix B of the specification recognizes on-the-ground reality of how code has behaved historically in the majority of JS engines/environments, particularly JS engines used by web browsers (including the engine used by Node.js). As such, while strict mode in the specification in proper restricts function declarations not at the top level of a script or function, Appendix B's "Block-Level Function Declarations Web Legacy Compatibility Semantics" modifies (reduces or removes) this restriction for the applicable JS environments. See alsoWhat is a module that returns a value back to the part of the program that called it?A value-returning function is a function that returns a value back to the part of the program that called it.
Which of the following is optional when writing a function definition?The parameter list that accepts arguments is optional in a function definition.
When a conversion function is used to convert a real number to an integer what usually happens to the real number's fractional part?The real to an integer conversion function accepts a real number as its argument and returns that number converted to an integer. If the real number has a fractional part, the fractional part will be thrown away.
Which function returns a string that is within another string?Returns a Variant (Long) specifying the position of the first occurrence of one string within another.
|