You are currently viewing JavaScript Cheat Sheet 10.47: JavaScript Use Strict

JavaScript Cheat Sheet 10.47: JavaScript Use Strict

Understanding JavaScript “use strict” Directive


In JavaScript, the “use strict” directive is a feature introduced in ECMAScript 5 (ES5) that allows you to opt into a stricter parsing and error handling mode. It helps to write cleaner, more secure, and less error-prone JavaScript code. In this tutorial, we’ll explore what “use strict” does, its benefits, and how to use it effectively in your JavaScript programs.

What is “use strict”?

    The “use strict” directive is a statement that enables strict mode in JavaScript. When you use strict mode, certain actions that were previously considered as silent errors will now throw exceptions, making debugging easier and code more robust. It also disables some features that are confusing or poorly thought out.

    Benefits of using “use strict”:

      • It catches common coding errors and throws exceptions.
      • It prevents the use of undeclared variables, reducing accidental global variable creation.
      • It makes debugging easier by providing more informative error messages.
      • It disables potentially problematic JavaScript features.

      How to use “use strict”:

        You can enable strict mode in two ways:

        a. Global strict mode:
        Place the “use strict” directive at the beginning of your JavaScript file or script tag in HTML.

           // Filename: script.js
           "use strict";
        
           // Your JavaScript code goes here

        b. Function-level strict mode:
        Place the “use strict” directive at the beginning of a function. In this case, strict mode applies only within that function’s scope.

           function myFunction() {
               "use strict";
        
               // Your strict mode code goes here
           }
        1. Examples and explanations:

        Let’s look at some examples to understand the effects of “use strict” in various scenarios.

        a. Example 1: Using undeclared variables

        "use strict";
        x = 10; // Throws a ReferenceError: x is not defined

        Explanation: In strict mode, assigning a value to an undeclared variable (variable that has not been declared with var, let, or const) will throw a ReferenceError.

        b. Example 2: Deleting undeletable properties

        "use strict";
        delete Object.prototype; // Throws a TypeError: Cannot delete property 'prototype' of object

        Explanation: In strict mode, attempting to delete a property that is not deletable (such as properties of built-in objects like Object.prototype) will throw a TypeError.

        c. Example 3: Using reserved keywords as variable names

        "use strict";
        var let = 5; // Throws a SyntaxError: Unexpected strict mode reserved word

        Explanation: In strict mode, using reserved keywords (such as let, static, implements, etc.) as variable names will throw a SyntaxError.

        d. Example 4: Duplicate property names in objects

        "use strict";
        var obj = { prop: 1, prop: 2 }; // Throws a SyntaxError: Duplicate data property in object literal not allowed in strict mode

        Explanation: In strict mode, defining objects with duplicate property names will throw a SyntaxError.

        Conclusion

          In this tutorial, we’ve learned about the “use strict” directive in JavaScript and its benefits. By using strict mode, you can write safer and more reliable JavaScript code. It’s recommended to always use strict mode in your JavaScript programs to catch errors early and make debugging easier. Remember to place the “use strict” directive at the beginning of your scripts to enable strict mode globally or within specific functions.

          Leave a Reply