Articles → JavaScript → Strict mode in JavaScript

Strict mode in JavaScript






What is strict mode?





Syntax


“use strict”



If undeclared variable is used in strict mode




<html>
<head>
    <title>Strict mode Demo</title>
    <script>
        "use strict";

        try {
            pi = 3.14;

            alert(pi);
        } catch (e) {
            alert(e);
        }
        

    </script>
</head>
<body>

</body>
</html>


Picture showing the exception message when variable is not declared in strict mode

Click to Enlarge


If variable are declared in strict mode


<html>
<head>
    <title>Strict mode Demo</title>
    <script>
        "use strict";

        try {
          var pi = 3.14;

            alert(pi);
        } catch (e) {
            alert(e);
        }
    </script>
</head>
<body>

</body>
</html>


Picture showing the output of the strict mode

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, June 02, 2020

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250