/

Understanding JavaScript Strict Mode

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
2
3
4
5
6
'use strict';

const name = 'Flavio';
const hello = () => 'hey';

//...

You can also enable Strict Mode for an individual function by including 'use strict' at the beginning of the function body:

1
2
3
4
function hello() {
'use strict';
return 'hey';
}

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.

  1. 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.

  2. 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 or Infinity will raise an error in Strict Mode.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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 the 0oXX syntax.

  9. Removed with: The with 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”]