title: Why doesn't my code work despite the placement of my external JavaScript file?
body: <p>This is the basis of how my document is written out. I have an external JavaScript file positioned at the top of the head element. Below it, I have code the references content from it. I would expect since the code is below the file it would work but it doesn't. It only works if I wrap the code in a <code>window.onload()</code> function. Or if I put the code in a script in the body element.</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;script type="text/javascript" src="myJavaScript.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
              console.log(w); // w is a variable inside myJavaScript.js but throws an error stating "w is not defined".
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;&lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Why does this code behave this way? I have the file written on top of the code that calls content within it yet it still doesn't work.</p>
  
  <p>EDIT: I found the error and it was that I wasn't defining the code in myJavaScript.js in the global scope. That's why the variable w never got through.</p>