This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

JavaScript Strict Mode


It was introduced in ECMAScript 5 to address to make it easier to write secure code. The JavaScript provides "use strict"; expression to enable the strict mode. If there is any silent error or mistake in the code, it throws an error.

you can include the following directive at the beginning of your JavaScript code:

'use strict';

When this directive is present, the JavaScript engine will enforce the following stricter rules:

  1. Variables must be declared before they are used.
  2. Assigning a value to an undeclared variable will result in a ReferenceError.
  3. Deleting a variable or function is not allowed.
  4. Duplicates in object literals are not allowed.
  5. The this keyword is not allowed to refer to the global object in function calls.
  6. The eval() function and the with statement are not allowed.
  7. Function parameters must have unique names.

These rules help to prevent common programming errors and security vulnerabilities, and also make it easier to optimize JavaScript code by allowing the engine to make certain assumptions about the code's behavior.

Here's an example of strict mode in action.

'use strict';
x = 10; // Uncaught ReferenceError: x is not defined
function foo(a, a) { // Uncaught SyntaxError: Duplicate parameter name not allowed in this context
  return a + a;
}

In this code, strict mode prevents the use of an undeclared variable x and a function with duplicate parameter names a. Instead of failing silently or producing unexpected behavior, strict mode causes these errors to be caught and reported as exceptions at runtime, making it easier to identify and fix the problems.


Prev Next