title: JavaScript global object vs window object
body: <p>Given the following code snippet.</p>
  
  <pre><code>var name = 'John';
  function foo() {
    console.log(this.name);
  }
  
  foo();
  </code></pre>
  
  <p>Why is it that when I run this code in the browser the log outputs the name, but when I run this same code snippet in node it outputs <code>undefined</code>?</p>
  
  <p>In the browser <code>this</code> refers to the window object, and the global variable will get attached to the window. Now in node <code>this</code> will refer to the global object in this example, so does my global variable not get attached to the global object as it does in the browser when it gets attached to the window? </p>