<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>pierrespring.com</title>
	
	<link>http://pierrespring.com</link>
	<description>geek, punk, pussy …</description>
	<lastBuildDate>Fri, 04 Mar 2011 15:35:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
	<copyright>Copyright © Pierre Spring 2010 </copyright>
	<managingEditor>pierre.spring@caillou.ch (Pierre Spring)</managingEditor>
	<webMaster>pierre.spring@caillou.ch (Pierre Spring)</webMaster>
	<ttl>1440</ttl>
	<image>
		<url>http://pierrespring.com/wp-content/uploads/2010/09/caillou.avatar.144x144.jpg</url>
		<title>pierrespring.com</title>
		<link>http://pierrespring.com</link>
		<width>144</width>
		<height>144</height>
	</image>
	<itunes:subtitle />
	<itunes:summary>This Podcast is mainly about technology. Whenever I meet clever people that are willing to sit down and talk to me, I try to get it on tape for you. It's going to be about things that are of interest to me: Open Source, Web development, PHP, JavaScript... </itunes:summary>
	<itunes:keywords>PHP, JavaScript, Programing, Development</itunes:keywords>
	<itunes:category text="Technology" />
	<itunes:author>Pierre Spring</itunes:author>
	<itunes:owner>
		<itunes:name>Pierre Spring</itunes:name>
		<itunes:email>pierre.spring@caillou.ch</itunes:email>
	</itunes:owner>
	<itunes:block>no</itunes:block>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://pierrespring.com/wp-content/uploads/2010/09/caillou.avatar.300x300.jpg" />
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/pierrespring" /><feedburner:info uri="pierrespring" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Discussion 3: phpBB</title>
		<link>http://feedproxy.google.com/~r/pierrespring/~3/rAEyn-nhSOM/</link>
		<comments>http://pierrespring.com/2011/03/04/discussion-3-phpbb/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 15:30:29 +0000</pubDate>
		<dc:creator>Pierre Spring</dc:creator>
				<category><![CDATA[Podcast]]></category>

		<guid isPermaLink="false">http://pierrespring.com/?p=183</guid>
		<description><![CDATA[This is a discussion about phpBB I had with Nils Adermann. Nils is the Lead Developer of phpBB and talks about the history and future of the bulletin board. It was recorded at the Symfony Live 2011 conference.]]></description>
			<content:encoded><![CDATA[<p><a style="float:right; background: url('http://macvox.ch/wp-content/uploads/2010/04/ituneslogo.png') no-repeat scroll right 0pt transparent; padding: 0pt 30px 20px 0pt;" target="itunes_store" href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=393864497">
      <img height="15" width="45" src="http://ax.phobos.apple.com.edgesuite.net/images/badgeitunes61x15dark.gif" alt="Pierre Spring Podcast">
    </a></p>

<p>This is a discussion about <a href="http://www.phpbb.com/">phpBB</a> I had with <a href="http://naderman.de">Nils Adermann</a>. Nils is the Lead Developer of phpBB and talks about the history and future of the bulletin board. It was recorded at the <a href="http://www.symfony-live.com/paris">Symfony Live 2011</a> conference.</p>
]]></content:encoded>
			<wfw:commentRss>http://pierrespring.com/2011/03/04/discussion-3-phpbb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
			<enclosure url="http://files.pierrespring.com/PS003.mp3" length="43363298" type="audio/mpeg" />
		<itunes:duration>0:45:10</itunes:duration>
		<itunes:subtitle>
      
    

This is a discussion about phpBB I had with Nils Adermann. Nils is the Lead Developer of phpBB and talks about the history and future of the bulletin board. It was recorded at the Symfony Live 2011 conference.</itunes:subtitle>
		<itunes:summary>
      
    

This is a discussion about phpBB I had with Nils Adermann. Nils is the Lead Developer of phpBB and talks about the history and future of the bulletin board. It was recorded at the Symfony Live 2011 conference.</itunes:summary>
		<itunes:keywords>Podcast</itunes:keywords>
		<itunes:author>Pierre Spring</itunes:author>
		<itunes:explicit>no</itunes:explicit>
		<itunes:block>no</itunes:block>
	<feedburner:origLink>http://pierrespring.com/2011/03/04/discussion-3-phpbb/</feedburner:origLink></item>
		<item>
		<title>Two Talks at Symfony Live 2011</title>
		<link>http://feedproxy.google.com/~r/pierrespring/~3/2WKoSMCCqO0/</link>
		<comments>http://pierrespring.com/2011/02/21/sflive2011/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 10:20:31 +0000</pubDate>
		<dc:creator>Pierre Spring</dc:creator>
				<category><![CDATA[Meta]]></category>

		<guid isPermaLink="false">http://pierrespring.com/?p=158</guid>
		<description><![CDATA[I am thrilled to announce that I&#8217;ll be talking at the Paris edition of the Symfony Live Conference in a couple of weeks! I have been introduced to symfony 1.4 a bit over a year ago and it was a revelation. I was about to jump over to Python in order to be able to [...]]]></description>
			<content:encoded><![CDATA[<p>I am thrilled to announce that I&#8217;ll be talking at the Paris edition of the <a href="http://www.symfony-live.com/paris">Symfony Live Conference</a> in a couple of weeks!</p>

<p>I have been introduced to symfony 1.4 a bit over a year ago and it was a revelation. I was about to jump over to <a href="http://www.python.org/">Python</a> in order to be able to use <a href="http://www.djangoproject.com/">Django</a> for my private projects when <a href="http://seld.be/">Jordi</a> and <a href="http://pooteeweet.org/">Lukas</a> basically force fed me symfony. It&#8217;s ease of use and the amazing documentation that came along made me forget all about Django. I spent several evenings going through the <a href="http://www.symfony-project.org/jobeet/1_4/Doctrine/en/">Jobeet</a> tutorial and I was ready to bootstrap my first webapp in just a few hours.</p>

<p>Due to its <a href="http://www.symfony-project.org/doc/1_4/">amazing documentation</a>, we got an entire team ready for productive work in under 2 days at <a href="http://liip.ch/">Liip</a>.</p>

<p><strong>Symfony2</strong></p>

<p>The next big thing will be <a href="http://symfony-reloaded.org/">Symfony2</a>. Symfony2 is a complete rewrite of the framework that will give your next project a strong foundation. <a href="http://components.symfony-project.org/dependency-injection/">Dependency injection</a> makes it completely testable and modular, it makes use of namespaces and supports <a href="http://www.slideshare.net/fabpot/caching-on-the-edge-with-symfony2">http-cache and ESI</a>. And there is much more!</p>

<p>Symfony2 is probably the one and only state of the art PHP framework you should consider these days. And if you want to learn more about it, join us at the Symfony Live 2011 in <a href="http://www.symfony-live.com/paris">Paris</a>.</p>

<p>I will be talking about <a href="http://www.symfony-live.com/paris/schedule#session-ps1">frontend optimization</a> and <a href="http://www.symfony-live.com/paris/schedule#session-ps2">PHP security</a> in Paris along with 29 of the <a href="http://www.symfony-live.com/paris/speakers">most interesting people in the PHP world</a>.</p>

<p>Don&#8217;t you think you should join us?</p>
]]></content:encoded>
			<wfw:commentRss>http://pierrespring.com/2011/02/21/sflive2011/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://pierrespring.com/2011/02/21/sflive2011/</feedburner:origLink></item>
		<item>
		<title>TechZing Compilation – Bootstrapping</title>
		<link>http://feedproxy.google.com/~r/pierrespring/~3/cIuSkE40erM/</link>
		<comments>http://pierrespring.com/2010/11/19/techzing-compilation-bootstrapping-a-web-app-business/#comments</comments>
		<pubDate>Fri, 19 Nov 2010 10:41:29 +0000</pubDate>
		<dc:creator>Pierre Spring</dc:creator>
				<category><![CDATA[Meta]]></category>

		<guid isPermaLink="false">http://pierrespring.com/?p=150</guid>
		<description><![CDATA[Justin Vincent from TechZing made a compilation of Podcasts around Bootstrapping a Web App Business. I wanted to download all of these episodes to listen to them on my iPod later. This is why I created an RSS feed of the compilation to use with iTunes: http://files.pierrespring.com/techzinglive_bootstrap.rss Simply add the feed url to Advanced > [...]]]></description>
			<content:encoded><![CDATA[<p>Justin Vincent from TechZing made a compilation of Podcasts around <a href="http://files.pierrespring.com/techzinglive_bootstrap.rss">Bootstrapping a Web App Business</a>.</p>

<p>I wanted to download all of these episodes to listen to them on my iPod later. This is why I created an RSS feed of the compilation to use with iTunes:</p>

<p><a href="http://files.pierrespring.com/techzinglive_bootstrap.rss">http://files.pierrespring.com/techzinglive_bootstrap.rss</a></p>

<p>Simply add the feed url to <strong>Advanced > Subscribe to Podcast…</strong> in iTunes and then click <strong>Get All</strong> as shown in the following image:</p>

<p><img src="http://pierrespring.com/wp-content/uploads/2010/11/subscribe.png" alt="" title="Subscribe in iTunes" width="550" height="379" class="alignright size-full wp-image-151" /></p>
]]></content:encoded>
			<wfw:commentRss>http://pierrespring.com/2010/11/19/techzing-compilation-bootstrapping-a-web-app-business/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://pierrespring.com/2010/11/19/techzing-compilation-bootstrapping-a-web-app-business/</feedburner:origLink></item>
		<item>
		<title>Discussion 2: Dokuwiki</title>
		<link>http://feedproxy.google.com/~r/pierrespring/~3/1lm73YqsnO0/</link>
		<comments>http://pierrespring.com/2010/11/13/discussion-2-dokuwiki/#comments</comments>
		<pubDate>Sat, 13 Nov 2010 21:04:51 +0000</pubDate>
		<dc:creator>Pierre Spring</dc:creator>
				<category><![CDATA[Podcast]]></category>

		<guid isPermaLink="false">http://pierrespring.com/?p=136</guid>
		<description><![CDATA[This is a discussion recorded at the FrOSCamp Zürich in September 2010. Andreas Gohr and Anika Henke talked about the beginnings of DokuWiki and what differentiates it from other wikis. I myself have used DokuWiki quite a lot. I feel like it&#8217;s the wiki that best suits my needs. As the name indicates, DokuWiki has [...]]]></description>
			<content:encoded><![CDATA[<p><a style="float:right; background: url('http://macvox.ch/wp-content/uploads/2010/04/ituneslogo.png') no-repeat scroll right 0pt transparent; padding: 0pt 30px 20px 0pt;" target="itunes_store" href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=393864497">
      <img height="15" width="45" src="http://ax.phobos.apple.com.edgesuite.net/images/badgeitunes61x15dark.gif" alt="Pierre Spring Podcast">
    </a></p>

<p>This is a discussion recorded at the <a href="http://froscamp.org/">FrOSCamp Zürich</a> in September 2010. <a href="http://splitbrain.org">Andreas Gohr</a> and <a href="http://blog.selfthinker.org/">Anika Henke</a> talked about the beginnings of <a href="http://dokuwiki.org">DokuWiki</a> and what differentiates it from other wikis.</p>

<p>I myself have used DokuWiki quite a lot. I feel like it&#8217;s the wiki that best suits my needs. As the name indicates, DokuWiki has put a focus at documenting things. I&#8217;ve use it to collect snippets of code over the years, to organize my travels, to write down ideas, ect. It&#8217;s the perfect digital notebook.</p>

<p>DokuWiki is very, very lightweight and has well thought event system in it&#8217;s core. This makes it super easy to modify pretty much any part of the wiki using a plugin. There is a massive amount of great plugins available on the <a href="http://www.dokuwiki.org/plugins">DokuWiki Plugins Page</a>.</p>

<p>Over the last couple of years, I&#8217;ve tried to write a <a href="https://github.com/caillou/dokuwysiwyg">WYSIWYG plugin for DokuWiki</a>. A project I&#8217;ve never quite completed until recently. If you&#8217;re interested in trying it out, you could <a href="https://github.com/caillou/dokuwysiwyg">fork it on GitHub</a>. It still needs a lot of work, so why not get involved?</p>

<p>Be sure to listen to the podcast and try out DokuWiki. It&#8217;s clean and simple! You will love it!</p>
]]></content:encoded>
			<wfw:commentRss>http://pierrespring.com/2010/11/13/discussion-2-dokuwiki/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
			<enclosure url="http://files.pierrespring.com/PS002.mp3" length="58202172" type="audio/mpeg" />
		<itunes:duration>0:00:01</itunes:duration>
		<itunes:subtitle>
      
    

This is a discussion recorded at the FrOSCamp Zürich in September 2010. Andreas Gohr and Anika Henke talked about the beginnings of DokuWiki and what differentiates it from other wikis.

I myself have used DokuWiki quite a lot. I feel [...]</itunes:subtitle>
		<itunes:summary>
      
    

This is a discussion recorded at the FrOSCamp Zürich in September 2010. Andreas Gohr and Anika Henke talked about the beginnings of DokuWiki and what differentiates it from other wikis.

I myself have used DokuWiki quite a lot. I feel like it’s the wiki that best suits my needs. As the name indicates, DokuWiki has put a focus at documenting things. I’ve use it to collect snippets of code over the years, to organize my travels, to write down ideas, ect. It’s the perfect digital notebook.

DokuWiki is very, very lightweight and has well thought event system in it’s core. This makes it super easy to modify pretty much any part of the wiki using a plugin. There is a massive amount of great plugins available on the DokuWiki Plugins Page.

Over the last couple of years, I’ve tried to write a WYSIWYG plugin for DokuWiki. A project I’ve never quite completed until recently. If you’re interested in trying it out, you could fork it on GitHub. It still needs a lot of work, so why not get involved?

Be sure to listen to the podcast and try out DokuWiki. It’s clean and simple! You will love it!</itunes:summary>
		<itunes:keywords>Podcast</itunes:keywords>
		<itunes:author>Pierre Spring</itunes:author>
		<itunes:explicit>no</itunes:explicit>
		<itunes:block>no</itunes:block>
	<feedburner:origLink>http://pierrespring.com/2010/11/13/discussion-2-dokuwiki/</feedburner:origLink></item>
		<item>
		<title>Discussion 1: Symfony2 CMF</title>
		<link>http://feedproxy.google.com/~r/pierrespring/~3/LtcPNn6pupI/</link>
		<comments>http://pierrespring.com/2010/09/20/ps001-discussion-symfony2-cmf/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 15:05:52 +0000</pubDate>
		<dc:creator>Pierre Spring</dc:creator>
				<category><![CDATA[Podcast]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://pierrespring.com/?p=104</guid>
		<description><![CDATA[In this very first episode of the podcast I met Lukas and Thibault in order to talk about the Symfony2 CMF. The Symfony CMF project makes it easier for developers to add CMS functionality to applications built with the Symfony2 PHP framework. Key development principles for the provided set of bundles are: scalability usability documentation [...]]]></description>
			<content:encoded><![CDATA[<p><a style="float:right; background: url('http://macvox.ch/wp-content/uploads/2010/04/ituneslogo.png') no-repeat scroll right 0pt transparent; padding: 0pt 30px 20px 0pt;" target="itunes_store" href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=393864497">
      <img height="15" width="45" src="http://ax.phobos.apple.com.edgesuite.net/images/badgeitunes61x15dark.gif" alt="Pierre Spring Podcast">
    </a></p>

<p>In this very first episode of the <strong>podcast</strong> I met Lukas and Thibault in order to talk about the <a href="http://cmf.symfony-project.org/">Symfony2 CMF</a>.</p>

<p>The Symfony CMF project makes it easier for <strong>developers</strong> to add <strong>CMS functionality</strong> to applications built with the <strong>Symfony2 PHP framework</strong>. Key development principles for the provided <strong>set of bundles</strong> are:</p>

<ul>
<li>scalability</li>
<li>usability</li>
<li>documentation</li>
<li>testing</li>
</ul>

<p>In this episode, we discussed the following things:</p>

<ul>
<li>Lukas: <a href="http://twitter.com/dybvandal">@dybvandal</a>, <a href="http://pooteeweet.org/">pooteeweet.org</a></li>
<li><a href="http://github.com/ornicar">Thibault on GitHub</a></li>
<li><a href="http://diem-project.org/">Diem Project</a></li>
<li><a href="http://symfony-reloaded.org/">Symfony2</a></li>
<li><a href="http://www.sympalphp.org/">Sympal</a></li>
<li><a href="http://www.apostrophenow.com/">Apostrophe</a></li>
<li><a href="http://punkave.com/">p&#8217;unk avenue</a></li>
<li>Symfony2 CMF

<ul>
<li><a href="http://cmf.symfony-project.org/">Website</a></li>
<li><a href="http://github.com/symfony-cmf/symfony-cmf">GitHub</a></li>
</ul></li>
<li><a href="http://en.wikipedia.org/wiki/Content_repository_API_for_Java">JCR</a></li>
<li><a href="http://jackrabbit.apache.org/">Jackrabbit</a></li>
<li><a href="https://fosswiki.liip.ch/display/jackalope/Home">Jackalope &#8211; JCR for PHP</a></li>
<li><a href="http://www.alfresco.com/">Alfresco</a></li>
<li><a href="http://en.wikipedia.org/wiki/Content_Management_Interoperability_Services">CMIS</a></li>
<li><a href="http://www.day.com">Day</a></li>
<li><a href="http://flow3.typo3.org/">FLOW3</a></li>
<li><a href="https://docs.google.com/document/pub?id=1GtXjjry6308og8NlXJl4scCGG1pC3FhoJs9bio0Q-Ys">FrOSCamp Meeting Notes</a></li>
<li><a href="http://github.com/symfony-cmf/symfony-cmf/issues/closed?label=initial%20voting#sort=votes">Initial Voting on CMF Feature</a></li>
<li><a href="http://en.wikipedia.org/wiki/Scrum_%28development%29">Scrum</a></li>
<li><a href="http://www.w3.org/TR/esi-lang">esi</a></li>
<li><a href="http://en.wikipedia.org/wiki/Varnish_%28software%29">Varnish</a></li>
<li><a href="http://en.wikipedia.org/wiki/Akamai_Technologies">Akamai</a></li>
<li><a href="http://lucene.apache.org/">Lucene</a></li>
<li><a href="http://framework.zend.com/manual/en/zend.search.lucene.html">Zend Search Lucene</a></li>
<li><a href="http://lucene.apache.org/solr/">Solr</a></li>
<li><a href="http://memcached.org/">Memcached</a></li>
<li><a href="http://www.symfonyday.com/">symfony Day</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://pierrespring.com/2010/09/20/ps001-discussion-symfony2-cmf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
			<enclosure url="http://files.pierrespring.com/PS001.mp3" length="49944869" type="audio/mpeg" />
		<itunes:duration>0:52:01</itunes:duration>
		<itunes:subtitle>
      
    

In this very first episode of the podcast I met Lukas and Thibault in order to talk about the Symfony2 CMF.

The Symfony CMF project makes it easier for developers to add CMS functionality to applications built with the Symfony2 PHP fr[...]</itunes:subtitle>
		<itunes:summary>
      
    

In this very first episode of the podcast I met Lukas and Thibault in order to talk about the Symfony2 CMF.

The Symfony CMF project makes it easier for developers to add CMS functionality to applications built with the Symfony2 PHP framework. Key development principles for the provided set of bundles are:


scalability
usability
documentation
testing


In this episode, we discussed the following things:


Lukas: @dybvandal, pooteeweet.org
Thibault on GitHub
Diem Project
Symfony2
Sympal
Apostrophe
p’unk avenue
Symfony2 CMF


Website
GitHub

JCR
Jackrabbit
Jackalope – JCR for PHP
Alfresco
CMIS
Day
FLOW3
FrOSCamp Meeting Notes
Initial Voting on CMF Feature
Scrum
esi
Varnish
Akamai
Lucene
Zend Search Lucene
Solr
Memcached
symfony Day
</itunes:summary>
		<itunes:keywords>Podcast</itunes:keywords>
		<itunes:author>Pierre Spring</itunes:author>
		<itunes:explicit>no</itunes:explicit>
		<itunes:block>no</itunes:block>
	<feedburner:origLink>http://pierrespring.com/2010/09/20/ps001-discussion-symfony2-cmf/</feedburner:origLink></item>
		<item>
		<title>Dirty looping in JavaScript</title>
		<link>http://feedproxy.google.com/~r/pierrespring/~3/1LKZrSlJXF4/</link>
		<comments>http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/#comments</comments>
		<pubDate>Mon, 24 May 2010 14:21:24 +0000</pubDate>
		<dc:creator>Pierre Spring</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://pierrespring.com/?p=78</guid>
		<description><![CDATA[Due to Function Scope and the hoisting of variable declarations we have to be a bit careful when using loops in JavaScript. Let&#8217;s have a look the following example which is inspired by Douglas Crockford&#8217;s 3rd video on JavaScript: var assure_positive = function &#40;matrix&#41; &#123; &#160; &#160; for &#40;var i = 0; i &#60; matrix.length; [...]]]></description>
			<content:encoded><![CDATA[<p>Due to <a href="/2010/05/11/function-scope-and-lexical-scoping/">Function Scope</a> and the <a href="/2010/05/20/my-variable-declaration-got-hoisted/">hoisting of variable declarations</a> we have to be a bit careful when using loops in JavaScript.</p>

<p>Let&#8217;s have a look the following example which is inspired by Douglas Crockford&#8217;s <a href="http://developer.yahoo.com/yui/theater/video.php?v=crockonjs-3">3rd video</a> on JavaScript:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> assure_positive <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>matrix<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> matrix.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; ^ fist declaration of i</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> row <span style="color: #339933;">=</span> matrix<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> row.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; ^ second declaration of i</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'MatrixError'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Negative'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>

<p>Due to the hoisting of variable declaration, this is the same as:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> assure_positive <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>matrix<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> row<span style="color: #339933;">,</span> i<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp; &nbsp; ^ declaration got hoisted</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> matrix.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; row <span style="color: #339933;">=</span> matrix<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> row.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// ^ i gets reset to 0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'MatrixError'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Negative'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>

<p>As you can see both loops share a single variable <code class="codecolorer javascript default"><span class="javascript">i</span></code>. This is certainly not what was intended by the programmer. A first error occurs if</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">matrix.<span style="color: #660066;">length</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">&gt;</span> row.<span style="color: #660066;">lenth</span></div></div>

<p>In this case the script will run indefinitely. In many other cases the function will simply produce false positives, as only the first row will be examined. Let&#8217;s test this:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> try_and_alert <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>matrix<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; assure_positive<span style="color: #009900;">&#40;</span>matrix<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Ok'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
try_and_alert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,-</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; alerts &quot;Negative&quot;</span><br />
try_and_alert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,-</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; alerts &quot;OK&quot; (false positive)</span><br />
try_and_alert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// -&gt; infinite loop</span></div></div>

<p>In addition to that, the <code class="codecolorer javascript default"><span class="javascript">row</span></code> variable is defined in the entire scope of the <code class="codecolorer javascript default"><span class="javascript">assure_positive</span></code> function. For now this is not harmful, but it could lead to future errors when refactoring the coed.</p>

<p>The described problem does not happen in languages with block scope, as each loop has it&#8217;s own scope. In order to achieve this in JavaScript, we simply need to wrap each loop into a function:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> assure_positive <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>matrix<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// wrapper around the loop over the matrix</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> matrix.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> row <span style="color: #339933;">=</span> matrix<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// wrapper around the loop over the row</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> row.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'MatrixError'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Negative'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
try_and_alert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,-</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; alerts &quot;Negative&quot;</span><br />
try_and_alert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,-</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; alerts &quot;Negative&quot;</span><br />
try_and_alert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// -&gt; alerts &quot;OK&quot;</span></div></div>

<p>Every variable that is declared within one of the wrapper functions is only visible in the scope of this function and all it&#8217;s sub-functions. It is invisible to the enclosing scope and due to <strong>Lexical Scoping</strong> (aka Closure) we still have access to the variables defined in the enclosing scopes, such as <code class="codecolorer javascript default"><span class="javascript">matrix</span></code> or <code class="codecolorer javascript default"><span class="javascript">row</span></code>.</p>

<p>Once again, we see how powerful Lexical Scoping is. It gives us the possibility to use functions in order to simulate block scope for our loops.</p>
]]></content:encoded>
			<wfw:commentRss>http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/</feedburner:origLink></item>
		<item>
		<title>My variable declaration got hoisted</title>
		<link>http://feedproxy.google.com/~r/pierrespring/~3/0srmVi3N9t0/</link>
		<comments>http://pierrespring.com/2010/05/20/my-variable-declaration-got-hoisted/#comments</comments>
		<pubDate>Thu, 20 May 2010 12:24:38 +0000</pubDate>
		<dc:creator>Pierre Spring</dc:creator>
				<category><![CDATA[Meta]]></category>

		<guid isPermaLink="false">http://pierrespring.com/?p=64</guid>
		<description><![CDATA[I already talked about function scope in my last blogpost. Yet to fully understand how functions scope works, it might be good to look at the hoisting of variable declarations. To non-native english speakers like me, the word hoisting itself presents a certain mystery. Here is what Wiktionary tells us: Verb to hoist: To raise; [...]]]></description>
			<content:encoded><![CDATA[<p>I already talked about function scope in my <a href="/2010/05/11/function-scope-and-lexical-scoping/">last blogpost</a>. Yet to fully understand how functions scope works, it might be good to look at the <strong>hoisting of variable
declarations</strong>.</p>

<p>To non-native english speakers like me, the word hoisting itself presents a certain mystery. Here is what <a href="http://en.wiktionary.org/wiki/hoist" title="Definition of hoist">Wiktionary</a> tells us:</p>

<blockquote>
  <p>Verb <strong>to hoist</strong>: To raise; to lift; to elevate; especially, to raise or lift to a desired elevation, by means of tackle or pulley, as a sail, a flag, a heavy package or weight.</p>
</blockquote>

<p>This is precisely what JavaScript does with a variable declaration.</p>

<p>The var statement</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span></div></div>

<p>gets
split into two statements:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// A var statement in which the variable counter is declared and</span><br />
<span style="color: #006600; font-style: italic;">// initialized to undefined.</span><br />
<span style="color: #003366; font-weight: bold;">var</span> counter<span style="color: #339933;">;</span> <br />
<br />
<span style="color: #006600; font-style: italic;">// An assignment statement which assigns the value 0 to the variable.</span><br />
counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span></div></div>

<p>When we say that the variable declaration gets hoisted, we mean that the declaration part of the <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span></span></code> statement is put to the beginning of the function by the interpreter, meaning that the two following functions are equal:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> f1 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// some code</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// some more code</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> f2 <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> counter<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// initialized with undefined</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// some code</span><br />
&nbsp; &nbsp;counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// some more code</span><br />
<span style="color: #009900;">&#125;</span></div></div>

<p><code class="codecolorer javascript default"><span class="javascript">f1</span></code> gets actually transformed into <code class="codecolorer javascript default"><span class="javascript">f2</span></code>.</p>

<p>Remember how we defined <strong>function scope</strong>: A variable declared <strong>anywhere</strong> in the function is visible <strong>everywhere</strong> in the function. The hoisting of variable declarations is the reason for this.</p>

<p>Hoisting of the variable declarations is also the reason we recommended to declare variables in the beginning of the function in JavaScript. This is different from most <code class="codecolorer javascript default"><span class="javascript">C</span></code> like languages, in which it is recommended to declare variables where they are actually used.</p>

<p>Let&#8217;s look at an example to see why declaring the variables at the top of a function is important for a better understanding of the program:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// a global variable</span><br />
<span style="color: #003366; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> f <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;counter<span style="color: #339933;">++;</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// so something</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> counter <span style="color: #339933;">&lt;</span> data.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> counter<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #006600; font-style: italic;">// do something</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
f<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>counter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; 0</span></div></div>

<p>This seems wired, right. The increment of <code class="codecolorer javascript default"><span class="javascript">counter<span style="color: #339933;">++</span></span></code> at the beginning of <code class="codecolorer javascript default"><span class="javascript">f<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> seems to be applied on the global <code class="codecolorer javascript default"><span class="javascript">counter</span></code> variable. But due to the hoisting of the declaration within the <code class="codecolorer javascript default"><span class="javascript"><span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">;;</span><span style="color: #009900;">&#41;</span></span></code> loop, the increment is applied to the <code class="codecolorer javascript default"><span class="javascript">counter</span></code> declared in the loop.</p>

<p>That&#8217;s it. I think that I coverd all the basics of <strong>function scope</strong> and <strong>lexical scoping</strong> in the last two posts. In my next post, I&#8217;ll show how to rewrite <code class="codecolorer javascript default"><span class="javascript">f</span></code> in order to be able to use a <code class="codecolorer javascript default"><span class="javascript">counter</span></code> variable within the loop and still have access to the <code class="codecolorer javascript default"><span class="javascript">counter</span></code> from the enclosing scope.</p>
]]></content:encoded>
			<wfw:commentRss>http://pierrespring.com/2010/05/20/my-variable-declaration-got-hoisted/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://pierrespring.com/2010/05/20/my-variable-declaration-got-hoisted/</feedburner:origLink></item>
		<item>
		<title>Of Function Scope and Lexical Scoping</title>
		<link>http://feedproxy.google.com/~r/pierrespring/~3/Foiljm4T8Fs/</link>
		<comments>http://pierrespring.com/2010/05/11/function-scope-and-lexical-scoping/#comments</comments>
		<pubDate>Tue, 11 May 2010 17:34:17 +0000</pubDate>
		<dc:creator>Pierre Spring</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lexical scoping]]></category>

		<guid isPermaLink="false">http://pierrespring.com/?p=48</guid>
		<description><![CDATA[As I was writing about loops in JavaScript I noticed that the problem was rather with <strong>Function Scope</strong> than with <em>Lexical Scoping</em>. And not being sure what the exact difference between <em>Function Scope</em> and <em>Lexical Scoping</em> was, I dropped my initial article in order to ask (and hopefully answer) the following questions:

What is <em>Lexical Scoping</em>? And how does it relate to <em>Function Scope</em>?]]></description>
			<content:encoded><![CDATA[<p>Initially I meant to write an article entitled &#8220;Dirty looping in JavaScript&#8221;. The tagline of this soon to be written article was the following:</p>

<blockquote>
  <p>Due to <a href="http://en.wikipedia.org/wiki/Scope_%28programming%29#Lexical_scoping">Lexical Scoping</a> and the way variables are declared and assigned values in JavaScript we have to be a bit careful when using loops.</p>
</blockquote>

<p>As I was writing about loops in JavaScript I noticed that the problem was rather with <strong>Function Scope</strong> than with <em>Lexical Scoping</em>. And not being sure what the exact difference between <em>Function Scope</em> and <em>Lexical Scoping</em> was, I dropped my initial article in order to ask (and hopefully answer) the following questions:</p>

<p>What is <em>Lexical Scoping</em>? And how does it relate to <em>Function Scope</em>?</p>

<p>To me, <em>Scoping</em> is the ruleset used to lookup variable values. Especially the ones that are not declared within the current <em>Scope</em>. Only when you exactly know how <em>Scoping</em> works in your language, can you be sure about the value a variable has at any given place in your code. It is very important to understand how this is implemented in your language of choice.</p>

<p>Wikipedia defines <em>Scope</em> as follows:</p>

<blockquote>
  <p>Typically, scope is used to define the extent of information hiding—that is, the visibility or accessibility of variables from different parts of the program.</p>
</blockquote>

<p>In JavaScript we have <em>Function Scope</em> and <em>Lexical Scoping</em>.</p>

<p><strong>Function Scope</strong> means that any variable which is defined within a function is visible within that <em>entire function</em>. This is quite different form <em>Block Scope</em>, in which a variable scope is limited by the <em>block</em> a variable is declared in. A block is usually what you find between <code class="codecolorer php default"><span class="php"><span style="color: #009900;">&#123;</span>curly braces<span style="color: #009900;">&#125;</span></span></code> or loop variables. <em>Block Scope</em> is used in most of the programming languages that are currently popular. Think of a <code class="codecolorer javascript default"><span class="javascript"><span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">;;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></span></code> loop:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// block scope &nbsp; &nbsp;-&gt; 1</span><br />
<span style="color: #006600; font-style: italic;">// function scope -&gt; 10</span></div></div>

<p>Pretty simple, right. Now we know what <em>Function Scope</em> is. But what is <em>Lexical Scoping</em>?</p>

<p><strong>Lexical Scoping</strong> defines how variable names are resolved in nested functions. Other names of <em>Lexical Scoping</em> are <em>Static Scoping</em> or <em>Closure</em>. It means that the scope of an <em>inner function</em> contains the scope of a <em>parent function</em>. Let&#8217;s have a look at the following example:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">'Look at me'</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> parent_function <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> inner_function <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> scream <span style="color: #339933;">=</span> <span style="color: #3366CC;">'!!!'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>text <span style="color: #339933;">+</span> <span style="color: #3366CC;">', '</span> <span style="color: #339933;">+</span> reason <span style="color: #339933;">+</span> scream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> reason <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;I'm an attention whore&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> scream <span style="color: #339933;">=</span> <span style="color: #3366CC;">'!'</span><span style="color: #339933;">;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>text <span style="color: #339933;">+</span> scream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; inner_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
parent_function<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #006600; font-style: italic;">// alerts the two following strings, in this order:</span><br />
<span style="color: #006600; font-style: italic;">// &quot;Look at me!&quot;</span><br />
<span style="color: #006600; font-style: italic;">// &quot;Look at me, I'm an attention whore!!!&quot;</span></div></div>

<p>Look at what happens when the <code class="codecolorer javascript default"><span class="javascript">inner_function</span></code> is called. It needs to alert all three of the following variables: <code class="codecolorer javascript default"><span class="javascript">text</span></code>, <code class="codecolorer javascript default"><span class="javascript">reason</span></code> and <code class="codecolorer javascript default"><span class="javascript">scream</span></code>.</p>

<p>The first variable that needs to be looked up is <code class="codecolorer javascript default"><span class="javascript">text</span></code>. This variable is neither declared in the scope of the <code class="codecolorer javascript default"><span class="javascript">inner_function</span></code> nor in the scope of <code class="codecolorer javascript default"><span class="javascript">parent_function</span></code>. So it takes the globally declared <code class="codecolorer javascript default"><span class="javascript">text</span></code>.</p>

<p>The second variable the <code class="codecolorer javascript default"><span class="javascript"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> in the <code class="codecolorer javascript default"><span class="javascript">inner_function</span></code> needs is <code class="codecolorer javascript default"><span class="javascript">reason</span></code>. This variable is not declared within the <code class="codecolorer javascript default"><span class="javascript">inner_function</span></code>. Hence it is looked for in the enclosing <code class="codecolorer javascript default"><span class="javascript">parent_function</span></code> scope where it finds the truth about my nature. <code class="codecolorer javascript default"><span class="javascript"><span style="color: #3366CC;">&quot;I'm an attention whore&quot;</span></span></code>.</p>

<p>The 3rd variable <code class="codecolorer javascript default"><span class="javascript">inner_function</span></code> needs to <code class="codecolorer javascript default"><span class="javascript"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> is <code class="codecolorer javascript default"><span class="javascript">scream</span></code>. It is declared within <code class="codecolorer javascript default"><span class="javascript">inner_function</span></code> and <code class="codecolorer javascript default"><span class="javascript">parent_function</span></code>. The <code class="codecolorer javascript default"><span class="javascript"><span style="color: #000066;">alert</span></span></code> is called in the scope of <code class="codecolorer javascript default"><span class="javascript">inner_function</span></code>, hence it is resolved to <code class="codecolorer javascript default"><span class="javascript"><span style="color: #3366CC;">'!!!'</span></span></code>.</p>

<p>That&#8217;s almost all you have to know about <em>Lexical Scoping</em>. <strong>The scope of an inner function contains the scope of a parent function.</strong></p>

<p>If you bare with me for a little more, you will know it all. There is a little more to it, and that little more is what makes <em>Lexical Scoping</em> so powerful: <em>inner functions</em> contain the scope of <em>parent functions</em> <strong>even if the parent function has returned.</strong></p>

<p>Let&#8217;s look at the following example:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> adder <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>total<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// the following function is returned </span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// and assigned to adder</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> inner_function <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>summand<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; total <span style="color: #339933;">+=</span> summand<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>total<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; <span style="color: #000066; font-weight: bold;">return</span> inner_function<span style="color: #339933;">;</span><br />
&nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &lt;- we call the annonymous function </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp; &nbsp;and assign the returned function to adder</span><br />
<br />
adder<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; 2</span><br />
adder<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; 5</span></div></div>

<p>This is possible, because in JavaScript <em>functions are first class objects</em>. That means that you can pass functions as arguments or they can be returned from a function. They can also be assigned to a variable or stored in an object. Anyway, the <code class="codecolorer javascript default"><span class="javascript">inner_function</span></code> that is returned is assigned to the variable <code class="codecolorer javascript default"><span class="javascript">adder</span></code>. And when you call <code class="codecolorer javascript default"><span class="javascript">adder</span></code>, that is the <code class="codecolorer javascript default"><span class="javascript">inner_function</span></code>, it has access to <code class="codecolorer javascript default"><span class="javascript">total</span></code> due to <em>Lexical Scoping</em>, even though the function that had the <code class="codecolorer javascript default"><span class="javascript">total</span></code>. <code class="codecolorer javascript default"><span class="javascript">total</span></code> itself was declared in the scope of a function that has returned a long time ago.</p>

<p>Get it? How sexy is that? Simple yet beautiful!</p>

<p>And that&#8217;s it. You know everything there is to know about <em>Lexical Scoping</em>: <strong>inner functions contain the scope of parent functions even if the parent function has returned.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://pierrespring.com/2010/05/11/function-scope-and-lexical-scoping/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://pierrespring.com/2010/05/11/function-scope-and-lexical-scoping/</feedburner:origLink></item>
		<item>
		<title>JavaScript values null and undefined</title>
		<link>http://feedproxy.google.com/~r/pierrespring/~3/YPdNNooUuWk/</link>
		<comments>http://pierrespring.com/2010/04/25/javascript-null-undefine/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 23:32:11 +0000</pubDate>
		<dc:creator>Pierre Spring</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://pierrespring.com/?p=13</guid>
		<description><![CDATA[In JavaScript everything is an Object. Everything but null and undefined. The first few times I read this, it didn&#8217;t really strike me. Simply because I had never seen or used null in my own JavaScript code. But having worked with the Google Maps API recently, I came across null and wondered what the difference [...]]]></description>
			<content:encoded><![CDATA[<p>In JavaScript everything is an Object. Everything but <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> and <code class="codecolorer javascript default"><span class="javascript">undefined</span></code>.</p>

<p>The first few times I read this, it didn&#8217;t really strike me. Simply because I had never seen or used <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> in my own JavaScript code. But having worked with the Google Maps API recently, I came across <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> and wondered what the difference between the two could be.</p>

<p>To be completely honest, I saw how one of my workmates used it. I shook my head, looked at him and told him the following thing in an annoyingly patronizing manner: &#8220;You know what, <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> isn&#8217;t part of the JavaScript language. In JavaScript we have <code class="codecolorer javascript default"><span class="javascript">undefined</span></code>, which is the same than <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> in PHP&#8221; He just pointed me to some code in the Google Maps API and smiled. I was baffled by both, my ignorance and the existence of <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code>. What could this <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> be. And how would it relate to <code class="codecolorer javascript default"><span class="javascript">undefined</span></code>?</p>

<p>Some investigation showed what most people would have derived from the names of both these language constructs.</p>

<p><code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> is a value. Or rather a non-value. It is the value you can assign to a variable or an object property, when you don&#8217;t know what else to assign. It is often used in order to intialize an object property.</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> child <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><br />
&nbsp; baptize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>

<p>In some cultures, like in Finland, this actually makes sense. Finnish children do not get a name before they are baptized. But this is an other story.</p>

<p>Now that you have a <code class="codecolorer javascript default"><span class="javascript">child</span></code> object, you might want to know if the <code class="codecolorer javascript default"><span class="javascript">child</span></code> has a name. As I mentioned in the beginning, <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> is not an object, so you&#8217;d expect to be able to test for something like <code class="codecolorer javascript default"><span class="javascript"><span style="color: #000066; font-weight: bold;">typeof</span> child.<span style="color: #000066;">name</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'null'</span></span></code>. Unfortunately, <code class="codecolorer javascript default"><span class="javascript"><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #003366; font-weight: bold;">null</span></span></code> returns <code class="codecolorer javascript default"><span class="javascript"><span style="color: #3366CC;">'object'</span></span></code>. This is an error in the specification of JavaScript. And it is not likely to change. As a result we end up with the following <code class="codecolorer javascript default"><span class="javascript">getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> function:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> child <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><br />
&nbsp; baptize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; getName<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'object'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">||</span> ‘Not named yet’<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
child.<span style="color: #660066;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; 'Not named yet.'</span><br />
child.<span style="color: #660066;">baptize</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Pierre'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
child.<span style="color: #660066;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; 'Pierre'</span><br />
child.<span style="color: #660066;">baptize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>first<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Frank'</span><span style="color: #339933;">,</span> middle<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Alvar'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
child.<span style="color: #660066;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; {first: 'Frank', middle: 'Alvar'}</span></div></div>

<p>So, even though <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> is not an object, <code class="codecolorer javascript default"><span class="javascript"><span style="color: #000066; font-weight: bold;">typeof</span> <span style="color: #003366; font-weight: bold;">null</span></span></code> returns <code class="codecolorer javascript default"><span class="javascript"><span style="color: #3366CC;">'object'</span></span></code>. But we can take advantage of the fact that every JavaScript object is typecasted to <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">true</span></span></code> and <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> is typecasted to <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">false</span></span></code>.</p>

<p>Now we know what <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> is. But what is <code class="codecolorer javascript default"><span class="javascript">undefined</span></code>?  <code class="codecolorer javascript default"><span class="javascript">undefined</span></code> is even less than <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code>. It is the default value for variables and parameters and the value of missing members in objects:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// default value for variables</span><br />
<span style="color: #003366; font-weight: bold;">var</span> lessThanNull<span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">typeof</span> lessThanNull<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt; 'undefined'</span><br />
<br />
<span style="color: #006600; font-style: italic;">// default value for parameters</span><br />
<span style="color: #003366; font-weight: bold;">var</span> getType <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">typeof</span> p<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
getType<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// -&gt; 'undefined'</span><br />
<br />
<span style="color: #006600; font-style: italic;">// value of missing members in objects</span><br />
<span style="color: #003366; font-weight: bold;">var</span> empty_object <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">typeof</span> empty_object.<span style="color: #660066;">notInitialized</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt;'undefined'</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> colors <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'red'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'green'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'orange'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">delete</span> colors<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">typeof</span> colors<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// -&gt;'undefined'</span></div></div>

<p>That&#8217;s it.</p>

<p>Just to compare it to PHP: Every time you get <code class="codecolorer javascript default"><span class="javascript">undefined</span></code> somewhere in your JavaScript code, PHP would return <code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">null</span></span></code> and trigger a Notice. I personally think that JavaScript has chosen the more elegant solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://pierrespring.com/2010/04/25/javascript-null-undefine/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://pierrespring.com/2010/04/25/javascript-null-undefine/</feedburner:origLink></item>
		<item>
		<title>Hello world!</title>
		<link>http://feedproxy.google.com/~r/pierrespring/~3/WFxUqTXAirE/</link>
		<comments>http://pierrespring.com/2010/04/10/hello-world/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 09:41:19 +0000</pubDate>
		<dc:creator>Pierre Spring</dc:creator>
				<category><![CDATA[Meta]]></category>

		<guid isPermaLink="false">http://pierrespring.com/?p=1</guid>
		<description><![CDATA[Here we go. After quite some time, I finally managed to go online with this blog. My plan is to write about the things I learn or discuss with my workmates, so it will most likely be about PHP, Javascript, HTML5, DBs and Servers. Anything we web app developers encounter daily. I will try to [...]]]></description>
			<content:encoded><![CDATA[<p>Here we go. After quite some time, I finally managed to go online with this blog. My plan is to write about the things I learn or discuss with my workmates, so it will most likely be about PHP, Javascript, HTML5, DBs and Servers. Anything we web app developers encounter daily.</p>

<p>I will try to write at least once a week. This should be more than enough, taken that I have a 100% job at <a href="http://liip.ch">Liip</a>, that I record at least one podcast a week (either for <a href="http://entrefaces.ch">entrefaces.ch</a> or <a href="http://macvox.ch">macvox.ch</a>), that I organize the <a href="http://webmardi.ch">webmardi</a> sessions once a month and that I try to be a somewhat present father of two and husband.</p>

<p>It seems a wee bit too much.</p>

<p>Anyway: Big thanks to <a href="http://sicher.ch">Stefan Sicher</a> for the design and <a href="http://meodai.ch/">David Aerne</a> for helping me with CSS.</p>
]]></content:encoded>
			<wfw:commentRss>http://pierrespring.com/2010/04/10/hello-world/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://pierrespring.com/2010/04/10/hello-world/</feedburner:origLink></item>
	</channel>
</rss>
