Strict Mode

Javascript strict mode has a different parsing and execution model. Some of the erratic behavior is addressed and errors are thrown for unsafe activities in strict mode. To enable strict mode for an entire script, include the following at the top:

"use strict";

This is a pragma that tells the JavaScript engines to change into strict mode.

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
  <script type="text/javascript">
     "use strict"; 
     /* var firseName = 'firseName';
      var thisIsAnotherVariable = 'this Is Another Variable';
    document.writeln(firseName);
    document.writeln(thisIsAnotherVariable);
     */  
  </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

You may specify a function to execute in strict mode:

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
  <script type="text/javascript">
        function doSomething(){ 
            "use strict"; 
            //function body 
        } 
        doSomething();
  </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Language Basics:
  1. Case-sensitivity
  2. Identifiers
  3. Comments
  4. Strict Mode
  5. Statements
  6. Keywords And Reserved Words
  7. Variables