title: Access value of variable from if statement Javascript with event onclick
body: <p>I have the following snippet of code:</p>
  
  <pre><code>var j=4;
      var x;
      if(j&gt;5){
          x=8;
      }else if(j&lt;5){
          x=1;
      }
      function write(){
          document.write(x);
      }
  </code></pre>
  
  <p>UPDATE: I am having a problem that when I have a button call the function write() it will not print out 1 when it is clicked, it will print out nothing. Calling the function write() separately works, but how do I get that to change for when it is called on by the onclick event?</p>