“Strict mode” is a feature in JavaScript that enables a stricter version of the JavaScript language, with additional checks and warnings to help you write safer and more robust code. When you use strict mode, certain language features that can be problematic or confusing in certain situations are disabled, and certain other behaviors are changed to be more consistent or to throw errors when unexpected conditions occur.

You can enable strict mode for an entire script by adding the string literal “use strict” at the beginning of the script, or for a specific function by adding the same string literal at the beginning of the function.

Example of Strict Mode

Here’s an example of how you might use strict mode in a script:

"use strict";
let x = 5;
let y = 10;
let sum = function(x, y) {
  return x + y;
};
console.log(sum(x, y)); // Output: 15

In this example, the script will be executed in strict mode because of the “use strict” directive. In strict mode, this script will raise error if you try to use undeclared variable, and some other features that can be problematic or confusing in certain situations are disabled.

It is recommended to use strict mode in production code as it helps to write safer and more robust code.

Advantages of Strict Mode in JavaScript

Strict mode in JavaScript has several advantages:

Preventing accidental global variables: In normal JavaScript, if you accidentally assign a value to an undeclared variable, JavaScript will automatically create a global variable with that name. In strict mode, this behavior is prevented and an error will be thrown instead.

Disabling ‘with’ statement: The ‘with’ statement can make code harder to understand and maintain, and can also cause performance issues. In strict mode, the ‘with’ statement is disabled.

Making ‘eval’ safer: The ‘eval’ function can be used to execute arbitrary code, which can be a security risk. In strict mode, the ‘eval’ function is slightly modified to be safer.

Preventing duplicate property names: In normal JavaScript, you can create an object with properties that have the same name. In strict mode, this behavior is prevented and an error will be thrown instead.

Making octal literals prohibited: Octal literals are a feature that can be confusing and hard to understand. In strict mode, octal literals are not allowed.

Making ‘arguments’ and ‘caller’ properties non-configurable: In normal JavaScript, the ‘arguments’ and ‘caller’ properties can be deleted, which can cause unexpected behavior. In strict mode, these properties are non-configurable, which means that they cannot be deleted.

Making ‘arguments’ and ‘caller’ properties non-writable: In normal JavaScript, the ‘arguments’ and ‘caller’ properties can be overwritten, which can cause unexpected behavior. In strict mode, these properties are non-writable, which means that they cannot be overwritten.

Making ‘delete’ operator more strict: In normal JavaScript, the ‘delete’ operator can be used to delete properties from objects, even if the properties are non-configurable. In strict mode, the ‘delete’ operator is more strict and will throw an error if the property is non-configurable.

Also Read:

Categorized in: