011_Strict keyword -must known

  • 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
  • Example: Using strict mode, don’t allow to use a variable without declaring it.

    // Objects are variables too.
    // Using an object, without declaring it, is not allowed:
    'use strict';
      
      // Will throw an error
      x = {p1:10, p2:20}; 
  • Output:
  • Example: Deleting a variable (or object) and a function is not allowed 
    'use strict';
     let x = 3.14;
      
    // Deleting a function is also not allowed
    'use strict';
     function x(p1, p2) {}; 
      
     // Will throw an error
     delete x;      
  • Output:
  • Example: Duplicating a parameter name is not allowed.

    'use strict';
      
     // Will throw an error
     function x(p1, p1) {};   
  • Output:
  • Example: Octal numeric literals are not allowed.

    'use strict';
      
     // Will throw an error
     let x = 010;       
  • Output:
  • Example: Escape characters are not allowed.

    'use strict';
      
     // Will throw an error
     let x = \010;     
  • Output:
  • Example: Writing to a read-only property is not allowed.
    'use strict';
     let obj = {};
     Object.defineProperty(obj, "x", {value:0, writable:false});
      
     // Will throw an error
     obj.x = 3.14;    
  • Output:
  • Example: Writing to a get-only property is not allowed.
    filter_none
    brightness_4
    'use strict';
     let obj = {get x() {return 0} };
      
     // Will throw an error
     obj.x = 3.14;     
  • Output:
  • Example: Deleting an undeletable property is not allowed.

    'use strict';
      
     // Will throw an error
     delete Object.prototype; 
  • Output:
  • Example: The string “eval” cannot be used as a variable.

    'use strict';
      
     // Will throw an error
     let eval = 3.14;  
  • Output:
    • Example: The string “arguments” cannot be used as a variable.
      'use strict';
        
       // Will throw an error
       let arguments = 3.14;    
    • Output:
    • Example: The with statement is not allowed.

      'use strict';
        
       // Will throw an error
       with (Math){x = cos(2)}; 
    • Output:
    Note: In function calls like f(), this value was the global object. In strict mode, it is now undefined. In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties.

Comments

Popular posts from this blog

JavaScript — Double Equals vs. Triple Equals - weird things on JavaScript

06_Understanding Execution Context and Execution Stack in Javascript

Creating and inserting element- part-2