title: Is it important to define a var when you can define in a function?
body: <p>I have questions about defining a var in Javascript.<br>
  <a href="http://jsbin.com/ravaqi/1/edit?html,js,output" rel="nofollow">Here is a JS Bin</a><br></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var newText=document.getElementById("myinput").value;
  
  document.getElementById("changebutton").onclick=function(){
    newText=document.getElementById("myinput").value;
    document.getElementById("firstdiv").innerHTML=newText;
  };</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;title&gt;JS Bin&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;input id="myinput" type="text" value="test" /&gt;
    &lt;button id="changebutton"&gt;Change!&lt;/button&gt;
    &lt;div id="firstdiv"&gt;This is some text&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p></p>
  
  <p>In this code, I want to show things next to the box where I can type things in. The code above works well, even without <code>var newText=document.getElementById("myinput").value;</code> in the first line of JS part.</p>
  
  <p>So is it necessary to define variable? How would it be in some other cases?</p>