Strict mode is a restricted variant of the language that makes silent failures throw and drops error-prone legacy features. The non-strict default is called sloppy mode.
What changes
- Assigning to an undeclared name throws
ReferenceErrorinstead of creating a global. - Writes that fail silently in sloppy mode throw
TypeError: assigning to a read-only or getter-only property, adding a property to a non-extensible object,deleteon a non-configurable property. thisis not boxed or replaced: a plain function call hasthis === undefined, and a primitivethisstays a primitive. Sloppy mode substitutesglobalThisor a wrapper object.argumentsis unmapped: it no longer aliases named parameters, andarguments.calleethrows.- Early (syntax) errors for:
with, legacy octal literals such as010, duplicate parameter names, assigning toevalorarguments, and future reserved words likepublicas identifiers. - Direct
evalgets its own variable scope instead of leakingvardeclarations into the caller.
When code is strict automatically
- Module code, always.
- Class bodies (every method, accessor, field and static block, even in a sloppy script).
- Code nested inside strict code.
Otherwise it is opt-in with a 'use strict' directive: in the directive prologue (the leading string-literal statements) of a script (whole file) or of a function (that function only). Function-body strictness applies to the function, not to the file around it.