FrontEnd

The Modern Mode “use Strict”

Use strict mode in JavaScript was added in the new ECMA5. strict mode enables certain JS code to behave differently semantically in order to make it more secure.

As such, browsers that do not support the use of strict mode execute code written in strict mode differently than the browsers that do.

This mode makes several changes to normal JavaScript semantics:

  1. Eliminates some JavaScript silent errors by changing them to throw errors.
  2. Fixes mistakes that make it difficult for JavaScript engines to perform optimizations.
  3. Prohibits some syntax likely to be defined in future versions of ECMAScript

Syntax for Strict Mode:

‘use strict’;
Note : strict mode can be applied to the entire script or to individual functions and modules.

Strict mode for scripts:
To invoke strict mode for an entire script, put the exact statement “use strict”; (or ‘use strict’;) before any other statements.

‘use strict’;
var v = “Hi! I’m a strict mode script!”;

Individual Function:

function strictFunc() {
   'use strict';
    var x = 5;
    console.log(x);
}

Using Strict Mode does the following:

  1. It is not allowed to create global variables by mistake. All variables and objects must be declared before use.
  2. It is not permissible to delete a variable or function.
  3. Assigning values to variables that would be silently ignored in normal circumstances turns into an error. For instance, assigning a value to NaN would throw an error.
  4. Deleting object properties that are undeletable also produces an error.
  5. It ensures that all parameters of a function are unique.
  6. It does not allow the use of Octal literals with a preceding zero (e.g. 0604), as well as Octal escape literals (e.g. \24).
  7. It prevents eval and argument from being used as variable names.
  8. The eval function is not permitted to create a new variable in the scope of which it is called.
  9. The with keyword cannot be used, making its use a syntax error.
  10. It is not allowed to write to get-only or read-only variables.
  11. Using the this keyword to reference the global document object is not allowed in order to make the script more secure. Instead, it returns undefined.

For Es12 New features, refer this link https://skolaparthi.com/?p=177

Loading

Translate ยป