JavaScript's strict mode, introduced in ECMAScript 5, optimizes code for JS engine, eliminates bad-syntax code, and helps in writing secure JavaScript. Let's take a slightly deeper dive into this concept.
"Use strict" mode in JS applies certain restrictions in the entire script or in a particular function it is invoked in. Just add "use strict";
to the beginning of a script or a function.
Q. What changes does use strict
make? The list is pretty exhaustive. But here are some important changes you'll notice. If you're in strict mode:
Using a variable/object, without declaring it, will throw an error.
Using duplicate parameters in any function or duplicate property names in any object will throw an error.
Any assignment that silently fails (does not throw any error/no feedback to the user) in normal code (i.e assignment to a non-writable global or property, assignment to a getter-only property, assignment to a new property on a non-extensible object) will throw an error.
Deleting a function or an undeletable property will throw an error.
Using a number that begins with 0 will throw an error.
Setting a property to a primitive value, will throw an error.
There are a number of situations that could unintentionally cause
this
to be bound to the global object. In strict mode, instead of boundingthis
toglobal
object, this gets bound to undefined.ECMAScript 5 added a list of reserved words that will be used in the future. If you use them as variables or arguments, the strict mode will throw an error.
Using
arguments.callee
,arguments.caller
,with
keyword,eval()
function, is forbidden.Et cetera, et cetera.
Refer to this MDN doc on the same for further information and clarity.
Happy coding :)
#10DaysofJSfundamentals (DAY 6)