Explain the concept of “strict mode” in JavaScript.

“Strict mode” is a feature in JavaScript that enforces stricter parsing and error handling, aiming to make JavaScript code more robust, eliminate certain error-prone behaviors, and help developers write cleaner code. When strict mode is enabled, the JavaScript interpreter applies a set of rules and throws more errors in specific situations.

To enable strict mode, you simply add the following directive at the beginning of your script or function:

“use strict”;

Now, let’s delve into the details of how strict mode affects JavaScript code:

Prevents the use of undeclared variables: 

In strict mode, using variables that are not explicitly declared with the var, let, or const keywords will result in a ReferenceError. In non-strict mode, such usage would implicitly create a global variable, which can lead to bugs and unexpected behavior.

Example:

“use strict”;

x = 10; // Throws a ReferenceError: x is not defined

Disallows deleting variables, functions, and function arguments: 

In strict mode, attempting to delete variables, functions, or function arguments will throw a SyntaxError. In non-strict mode, these operations would simply fail silently.

Example:

“use strict”;

var x = 10;

delete x; // Throws a SyntaxError: Delete of an unqualified identifier in strict mode

Prohibits duplicate parameter names: In strict mode, declaring function parameters with duplicate names is not allowed and will result in a SyntaxError. Non-strict mode allows duplicate parameter names, with the latter parameter overwriting the former.

Example:

“use strict”;

function foo(a, b, a) {

  // Throws a SyntaxError: Duplicate parameter name not allowed in this context

}

Restricts the use of this in non-method functions: 

In strict mode, when a function is not called as a method or a constructor, the value of this inside the function will be undefined, whereas in non-strict mode, it would refer to the global object (window in a browser or global in Node.js). This helps prevent accidental use of the global object.

Example:

“use strict”;

function foo() {

  console.log(this); // Output: undefined

}

foo();

Forbids octal literals and octal escape sequences: Octal literals (e.g., 0123) and octal escape sequences (e.g., \012) are not allowed in strict mode. In non-strict mode, these constructs are treated as octal values.

Example:

“use strict”;

var x = 0123; // Throws a SyntaxError: Octal literals are not allowed in strict mode

These are just a few examples of the effects of strict mode. Strict mode also introduces other behavior changes related to how eval() works, how errors are thrown for invalid assignments, and more.

Enabling strict mode is generally recommended in modern JavaScript development as it helps catch common coding mistakes, promotes cleaner code practices, and improves code reliability. It’s important to note that strict mode operates at the script or function level, so it needs to be applied to each individual script or function where its effects are desired.