Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Strict Mode

JS Strict Mode

"use strict"; defines that JavaScript code should be executed in "strict mode".

The "use strict" Directive

"use strict" is not a statement, but a literal expression, ignored by earlier versions of JavaScript.

The purpose of "use strict" is to indicate that the code should be executed in "strict mode".

You can use "strict mode" in all your programs. It helps you to write cleaner code, like preventing you from using undeclared variables.

Declaring Strict Mode

Strict mode is declared by adding "use strict"; to the beginning of a script or a function.

Declared at the beginning of a script, it has global scope (all code in the script will execute in strict mode).

Declared inside a function, it has local or function scope (only the code inside the function is in strict mode)

Beginning Of Script (Global Scope)
					 
        
          "use strict";
          x = 3.14; // Error: x is not declared
        
      
Beginning Of Function (Local/Function Scope)
					 
        
          a = 3.14; // No error
          myFunction();

          function myFunction() {
            "use strict";
            b = 3.14;   // Error: y is not declared
          }
        
      
Why Strict Mode?

Strict mode makes it easier to write "secure" JavaScript. Strict mode changes previously accepted "bad syntax" into real errors.

As an example, in normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.

In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties.

In strict mode, any assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object, will throw an error.

Restrictions In Strict Mode

Using a variable or object, without declaring it, is not allowed.

					 
        
          "use strict";
          a = 3.14; // Error
          b = {p1:10, p2:20}; // Error
        
      

Deleting a variable or object is not allowed.

					 
        
          "use strict";
          let a = 3.14;
          let b = {p1:10, p2:20};
          delete a; // Error
          delete b; // Error
        
      

Deleting a function is not allowed.

					 
        
          "use strict";
          function myFun(p1, p2) {};
          delete myFun; // Error
        
      

Duplicating a parameter name is not allowed.

					 
        
          "use strict";
          function x(p1, p1) {}; // Error
        
      

Octal numeric literals and Octal escape characters are not allowed

					 
        
          "use strict";
          let a = 010; // Error
          let b = "\010"; // Error
        
      

Writing to a read-only property is not allowed.

					 
        
          "use strict";
          const obj = {};
          Object.defineProperty(obj, "x", {value:0, writable:false});
          obj.x = 3.14; // Error
        
      

Writing to a get-only property is not allowed.

					 
        
          "use strict";
          const obj = {};
          const obj = {get a() {return 0} };
          obj.a = 3.14;  // Error
        
      

Deleting an undeletable property is not allowed.

					 
        
          "use strict";
          delete Object.prototype; // Error
        
      

The words eval, arguments cannot be used as a variable.

					 
        
          "use strict";
          let eval = 3.14; // Error
          let arguments = 3.14; // Error
        
      

The with statement is not allowed.

					 
        
          "use strict";
          with (Math){x = cos(2)}; // Error
        
      

For security reasons, eval() is not allowed to create variables in the scope from which it was called.

					 
        
          "use strict";
          eval ("x = 2");
          alert (x); // Error
        
      

In strict mode, eval() can not declare a variable using the var or let keyword.

					 
        
          "use strict";
          eval ("var a = 2");
          eval ("let b = 2");
          alert (a); // Error
          alert (b); // Error
        
      

The this keyword in functions behaves differently in strict mode. The this keyword refers to the object that called the function.

If the object is not specified, functions in strict mode will return undefined and functions in normal mode will return the global object (window)

					 
        
          "use strict";
          function myFunction() {
            alert(this); // alert "undefined"
          }
          myFunction();
        
      
Future Restrictions

Keywords reserved for future JavaScript versions can NOT be used as variable names in strict mode.

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved