0

I am trying to write AngularJS and JavaScript function but I have a question about the keyword 'use strict'. Say the code is something like below kept in a html file within script tag. Now remember we are using 'use strict' keywords. And window.x is not defined anywhere within the html. When I load the HTML file on Google Chrome why does it not complain about window.x? Isn't it undefined? It outputs as window.x: undefined. So shouldn't any variable that is not defined be detected by AngularJS and spew out as an error in the Google Chrome Console or something? Why doesn't Chrome complain about the undefined variable?

(
  function() {
    'use strict';
    var x = "hello";

    console.log("window.x:", window.x); //undefined
    console.log("x:", x); //hello
    angular.module('myFirstApp', [])
      .controller('MyFirstController', function() {

      });
  })();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

1 Answers1

0

'use strict' does not check for property existence/access (which window.x is trying to find a property x on window) - that would be way too costly. Instead, it ensures that variables are declared.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445