Strict mode in JavaScript

What do you mean by strict mode in javascript and characteristics of javascript strict-mode?

In ECMAScript 5, a new feature called JavaScript Strict Mode allows you to write a code or a function in a "strict" operational environment. In most cases, this language is 'not particularly severe' when it comes to throwing errors. In 'Strict mode,' however, all forms of errors, including silent errors, will be thrown. As a result, debugging becomes a lot simpler. Thus programmer's chances of making an error are lowered.

Characteristics of strict mode in javascript

  1. Duplicate arguments are not allowed by developers.

  2. In strict mode, you won't be able to use the JavaScript keyword as a parameter or function name.

  3. The 'use strict' keyword is used to define strict mode at the start of the script. Strict mode is supported by all browsers.

  4. Engineers will not be allowed to create global variables in 'Strict Mode.

\=> Defining with examples

  1. Duplicate arguments are not allowed:

    In non-strict mode, it is possible to have duplicate arguments without any error. However, strict mode prohibits duplicate arguments in function definitions. For example:

// non-strict mode
function addNumbers(a, b, a) {
  return a + b + a;
}

console.log(addNumbers(2, 3, 4)); // 9

// strict mode
"use strict";
function addNumbers(a, b, a) {
  return a + b + a;
}

// Error: Duplicate parameter name not allowed in strict mode
  1. Reserved keywords as variable names are not allowed:

    In strict mode, using any of the JavaScript reserved keywords as variable names or function names is not allowed. In non-strict mode, this is not considered an error, and the code will run without any issue. For example:

     // non-strict mode
     var delete = 1;
     console.log(delete); // 1
    
     // strict mode
     "use strict";
     var delete = 1;
     // Error: delete is a reserved keyword in strict mode
    
  2. 'use strict' keyword:

    To enable strict mode, you need to add the 'use strict' directive at the beginning of your script or function. This directive is a string literal that tells the JavaScript engine to run the code in strict mode. For example:

     "use strict";
    
     // strict mode code
     function add(a, b) {
       return a + b;
     }
    
  3. No global variables allowed:

    In strict mode, you cannot create a global variable without declaring it explicitly using the var, let, or const keyword. In non-strict mode, undeclared variables become global variables automatically. For example:

     // non-strict mode
     function test() {
       x = 10;
     }
     test();
     console.log(x); // 10
    
     // strict mode
     "use strict";
     function test() {
       x = 10;
     }
     test();
     // Error: x is not defined
    

In summary, strict mode in JavaScript helps programmers to write more robust and error-free code. It enforces stricter rules on JavaScript code, preventing common mistakes and pitfalls.

Hope I was able to clear my points , thanks for reading!