Last active
          July 21, 2017 18:46 
        
      - 
      
 - 
        
Save tekaratzas/e7e2e01769ca72c2a692fd470f07b110 to your computer and use it in GitHub Desktop.  
    Exploring Scopes And namespaces in JavaScript
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var foo = 12; | |
| function changeFoo() { | |
| foo = 34; // changes global scope and not local scope! | |
| } | |
| changeFoo(); | |
| console.log(foo); | |
| // This becomes a more apparent issue in next example | |
| // Out here is the global scope | |
| for(var i = 0; i < 10; i++) { | |
| innerLoop(); | |
| } | |
| function innerLoop() { | |
| // this is a different scope | |
| for(i = 0; i < 10; i++) { // missing var statement! i refers to global scope! | |
| console.log("this is will show 10 times and not 100."); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment