Understanding JavaScript Strict Mode
JavaScript Strict Mode is an important feature introduced in ES5 that allows JavaScript to behave in a more controlled and predictable manner. By enabling Strict Mode, you can change the semantics of the JavaScript language and prevent common mistakes that can lead to bugs or unexpected behavior.
Enabling Strict Mode
Strict Mode is optional and not enabled by default as it could potentially break existing JavaScript code. To enable Strict Mode, you need to include the 'use strict'
directive at the beginning of your code.
You can include it at the beginning of a file to apply it to all the code within the file:
1 | ; |
You can also enable Strict Mode for an individual function by including 'use strict'
at the beginning of the function body:
1 | function hello() { |
This is useful when dealing with legacy code where enabling Strict Mode for the entire file may not be feasible.
What changes in Strict Mode
Here are some key differences when using Strict Mode compared to “normal” JavaScript, sometimes referred to as sloppy mode.
Accidental global variables: In normal JavaScript, if you assign a value to an undeclared variable, it is automatically created as a global variable. In Strict Mode, this behavior is changed, and an error is thrown instead.
Assignment errors: JavaScript has some implicit conversion of certain types. In Strict Mode, these implicit conversions that can result in errors are disallowed. For example, assigning a value to
undefined
orInfinity
will raise an error in Strict Mode.Read-only properties: In normal JavaScript, you can override read-only properties of an object. However, in Strict Mode, this behavior is disallowed, and an error will be thrown if you attempt to modify a read-only property.
Extension of non-extensible objects: In normal JavaScript, you can extend non-extensible objects by adding new properties to them. In Strict Mode, this behavior is disallowed, and an error will be thrown if you try to add properties to a non-extensible object.
Setting properties on primitive values: In normal JavaScript, you can set properties on primitive values without any effect. In Strict Mode, this behavior is changed, and attempting to set properties on primitive values will result in an error.
Deletion errors: In Strict Mode, trying to delete undeletable properties (such as built-in object prototypes) will throw an error, whereas in normal JavaScript, it would return false.
Function arguments with the same name: In normal JavaScript, you can have duplicate parameter names in a function declaration. In Strict Mode, this behavior is disallowed, and a
SyntaxError
will be thrown.Octal syntax: In normal JavaScript, an octal number can be represented by prefixing it with a
0
. In Strict Mode, this syntax is not allowed, and an error will be thrown. However, you can still use octal numbers in Strict Mode by using the0oXX
syntax.Removed
with
: Thewith
keyword is disabled in Strict Mode to avoid some edge cases and allow for better optimization at the compiler level.
By understanding these differences, you can write more robust and reliable JavaScript code using Strict Mode.
tags: [“JavaScript”, “Strict Mode”, “ES5”, “sloppy mode”, “breaking change”]