Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of strict mode in javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 282
    Comment on it

    strict mode:

    It is used to catch or throw errors when some unsafe actions are taken and  use strict directive is used to execute javascript code in strict mode. For example: we cannot use undeclared variable in strict mode.

    use strict can be used only at the beginning of a function  or of a script.

    How to declare strict mode:

    If we write use strict inside a function then only that function will execute in strict mode and if we have added in first line of script then whole script will execute in strict mode.

    Example 1:

    
    <script>
    x = 21;        // This will not cause an error.
    myFunc();
    
    function myFunc() {
        "use strict";
        y = 21;    /* This will cause an error (y is not defined) as we have used strict mode in this function.*/
    }
    </script>

    As in the above script we used use strict inside myFunc()  which means only myFunc() code  will execute in strict mode.

    Example 2:

    <script>
    "use strict";
    myFunc();
    
    function myFunc() {
        y = 21;   // This will cause an error (y is not defined)
    }
    </script>

    As in the above script we used use strict in the first line of script which means the whole script will execute in strict mode.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: