JavaScript Strict Mode

    Using the "use strict" statement we can activate the strict mode for our JavaScript code.

    JavaScript Strict Mode Overview

    The Strict Mode introduced in JavaScript ECMAScript version 5. And we can activate it for our script using the "use strict" statement. Unlike other JavaScript code it is not a general statement, but a literal expression that eliminates some of the dynamic features of JavaScript and makes sure that the JS developer writes the rest of the code inline to static programming.

    Using the Strict mode for your JS program is optional, professional developers use strict mode to make the code more readable and static so it could become easier to read and understand. Here is the list of some changes that strict mode bring to the JS script

    • If the strict mode on, the JS interpreter will throw some of the silent errors.
    • It also fixes the mistakes and optimizes the code for better performance.
    • It prohibits some of the dynamic syntaxes.

    JS Declare Strict Mode

    We can apply the strict mode to the complete script or to an individual function. To activate a Strict mode for the complete script or a function, we need to write the "use strict" statement before any other statement.

    Strict Mode for the Complete script

    To activate the strict mode for the complete script we need to write the "use strict"; statement at the top of the script. Example

    <script>
       "use strict";
       let x = "Strict mode is on";
    </script>

    Strict Mode for functions

    To invoke strict mode only for an individual function we need to write the "use strict "; statement inside the function body before writing any other function statement.

    Example

    <script>
       function strict_mode()
       {
        "use strict";
        var x = "Strict mode is on";
       }
    </script>

    Why use Strict mode in JavaScript?

    strict mode makes sure that the developer writes the "secure" JavaScript code. As JavaScript is a loosely typed language, with strict mode activated we can not define undeclared variables, and hoisting is also only applied to the var variables. The normal JavaScript creates a new variable if we misspelled an existing declared variable, but with strict mode activated we eliminate these types of bugs in JavaScript.

    Strict Mode changes

    1. With strict mode on we can not use a variable without declaring it.

    Example

    <script>
        "use strict";
        a =20;  //error  Uncaught ReferenceError: a is not defined
    </script>

    2. We can not delete a variable and function using the delete keyword when strict mode is on.

    Example

    <script>
        "use strict";
        var a =20;  
        delete a;    //Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.
    </script>

    3. We can not duplicate parameter names.

    Example

    <script>
        "use strict";
        function a(para1, para1)    //Uncaught SyntaxError: Duplicate parameter name not allowed in this context
        {
            //function body
        }
    </script>

    4. We can not use octal literal in Strict mode.

    Example

    <script>
        "use strict";
        var oc = 023;    //Uncaught SyntaxError: Octal literals are not allowed in strict mode.
    </script>

    5. We can not use octal escape characters .

    Example

    <script>
       "use strict";
       var oc = "\023";   //Uncaught SyntaxError: Octal escape sequences are not allowed in strict mode.
    </script>

    6. We can not write the read-only properties.

    Example

    <script>
        "use strict";
        var o = {};
        Object.defineProperty(o, "a", {value:2, writable:false});
        o.a= 20;  //Uncaught TypeError: Cannot assign to read only property 'a' of object '#<Object>'
    </script>

    7. We can not write to a get only property.

    Example

    <script>
        "use strict";
        var o = {get a(){return 2}};
        o.a= 20;   //Uncaught TypeError: Cannot set property a of #<Object> which has only a getter
    </script>

    8. The words arguments and eval can not be used as variables.

    Example

    <script>
        "use strict";
        var arguments = 20;   //Uncaught SyntaxError: Unexpected eval or arguments in strict mode
    </script>

    9. We can not use the with statement.

    Example

    <script>
        "use strict";
        with (Math) {a= sin(30)};   //Strict mode code may not include a with statement
    </script>

    10. In strict mode the this keyword refers to the object that called the function.

    Example

    <script>
        "use strict";
        function fun()
        {
            console.log(this);
        }
        fun();  //undefined
    </script>

    Summary

    • The "use strict" statement invokes the strict mode.
    • The strict mode can be applied to the complete script or to an individual function.
    • With strict mode, we can write mode secure code.

    People are also reading: