<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - The importance of var in JavaScript Changes</title>
  <id>tag:opensoul.org,2009:/2008/9/4/the-importance-of-var-in-javascript/changes</id>
  <generator version="0.8.0" uri="http://mephistoblog.com">Mephisto Drax</generator>
  <link href="http://opensoul.org/2008/9/4/the-importance-of-var-in-javascript/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2008/9/4/the-importance-of-var-in-javascript" rel="alternate" type="text/html"/>
  <updated>2008-09-04T00:44:25Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2008-09-04:478:2</id>
    <published>2008-09-04T00:37:00Z</published>
    <updated>2008-09-04T00:44:25Z</updated>
    <link href="http://opensoul.org/2008/9/4/the-importance-of-var-in-javascript" rel="alternate" type="text/html"/>
    <title>The importance of var in JavaScript</title>
<content type="html">&lt;p&gt;I would consider myself proficient at JavaScript. Not a rockstar, but I can hold my own. But I didn&#8217;t learn it out of a book; I picked it up slowly over several years. So occasionally I come across something that I probably would have learned in the first couple chapters of a decent JavaScript book.&lt;/p&gt;


	&lt;p&gt;Today, I experienced the importance of declaring variables with &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;For the unenlightened like myself, JavaScript basically has two scopes for variables: global and local. Variables assigned outside of a function are global, and variables assigned inside of a function, using the &lt;code&gt;var&lt;/code&gt; keyword, are local (not rocket surgery). However, if you leave the &lt;code&gt;var&lt;/code&gt; keyword off, it assigns a global variable, regardless of where it&#8217;s declared.&lt;/p&gt;


	&lt;p&gt;Here&#8217;s a bit of code that demonstrates this:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;function fail() {
  date = new Date();
  console.log('Before: ' + Number(date));
  setTimeout(function() { console.log('After: ' + Number(date)); }, 1000);
}

fail();
fail();&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The code defines a function that sets a variable, logs the variable, then defines a callback that logs the variable again on second later. If you call the function twice in a row, you can see the effect:&lt;/p&gt;


&lt;pre&gt;
Before: 1220487263486
Before: 1220487263499
After: 1220487263499 &amp;lt;- This should be 1220487263486
After: 1220487263499
&lt;/pre&gt;

	&lt;p&gt;Declaring the &lt;code&gt;date&lt;/code&gt; variable with &lt;code&gt;var&lt;/code&gt; gives us this output:&lt;/p&gt;


&lt;pre&gt;
Before: 1220487287985
Before: 1220487287994
After: 1220487287985 &amp;lt;- see, this is the same as the first line
After: 1220487287994
&lt;/pre&gt;

	&lt;p&gt;So remember kids, declare local variables with &lt;code&gt;var&lt;/code&gt;!&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2008-09-04:477:1</id>
    <published>2008-09-04T00:37:12Z</published>
    <updated>2008-09-04T00:37:12Z</updated>
    <link href="http://opensoul.org/2008/9/4/the-importance-of-var-in-javascript" rel="alternate" type="text/html"/>
    <title>The importance of var in JavaScript</title>
<content type="html">&lt;p&gt;I would consider myself proficient at JavaScript. Not a rockstar, but I can hold my own. But I didn&#8217;t learn it out of a book; I picked it up slowly over several years. So occasionally I come across something that I probably would have learned in the first couple chapters of a decent JavaScript book.&lt;/p&gt;


	&lt;p&gt;Today, I experienced the importance of declaring variables with &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;For the unenlightened like myself, javascript basically has two scopes for variables: global and local. Variables assigned outside of a function are global, and variables assigned inside of a function, using the &lt;code&gt;var&lt;/code&gt; keyword, are local (not rocket surgery). However, if you leave the &lt;code&gt;var&lt;/code&gt; keyword off, it assigns a global variable, regardless of where it&#8217;s declared.&lt;/p&gt;


	&lt;p&gt;Here&#8217;s a bit of code that demonstrates this:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;function fail() {
  date = new Date();
  console.log('Before: ' + Number(date));
  setTimeout(function() { console.log('After: ' + Number(date)); }, 1000);
}

fail();
fail();&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The code defines a function that sets a variable, logs the variable, then defines a callback that logs the variable again on second later. If you call the function twice in a row, you can see the effect:&lt;/p&gt;


&lt;pre&gt;
Before: 1220487263486
Before: 1220487263499
After: 1220487263499 &amp;lt;- This should be 1220487263486
After: 1220487263499
&lt;/pre&gt;

	&lt;p&gt;Declaring the &lt;code&gt;date&lt;/code&gt; variable with &lt;code&gt;var&lt;/code&gt; gives us this output:&lt;/p&gt;


&lt;pre&gt;
Before: 1220487287985
Before: 1220487287994
After: 1220487287985 &amp;lt;- see, this is the same as the first line
After: 1220487287994
&lt;/pre&gt;

	&lt;p&gt;So remember kids, declare local variables with &lt;code&gt;var&lt;/code&gt;!&lt;/p&gt;</content>  </entry>
</feed>
