<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Easy loop for every element of an array</title>
	<atom:link href="http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Fri, 30 Jul 2010 18:10:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Peter</title>
		<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/comment-page-1/#comment-852</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Wed, 09 Dec 2009 15:59:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=85#comment-852</guid>
		<description>The third way of doing it is just standard programming practice. It&#039;s how I&#039;ve always done it in JavaScript, C or any similar language!</description>
		<content:encoded><![CDATA[<p>The third way of doing it is just standard programming practice. It&#8217;s how I&#8217;ve always done it in JavaScript, C or any similar language!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Asnawi</title>
		<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/comment-page-1/#comment-838</link>
		<dc:creator>Asnawi</dc:creator>
		<pubDate>Wed, 28 Oct 2009 09:19:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=85#comment-838</guid>
		<description>If u have a false or null value in the array, u could use typeof like this:

var items = [&quot;a&quot;, &quot;b&quot;, false, &quot;c&quot;, null];
for (var i = 0, item; typeof (item = items[i]) != &#039;undefined&#039;; i++) {
  alert(item);
}

but u still have problem if the array contains undefined value, like:

var items = [&quot;a&quot;, undefined, &quot;b&quot;];</description>
		<content:encoded><![CDATA[<p>If u have a false or null value in the array, u could use typeof like this:</p>
<p>var items = ["a", "b", false, "c", null];<br />
for (var i = 0, item; typeof (item = items[i]) != &#8216;undefined&#8217;; i++) {<br />
  alert(item);<br />
}</p>
<p>but u still have problem if the array contains undefined value, like:</p>
<p>var items = ["a", undefined, "b"];</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Binny V A</title>
		<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/comment-page-1/#comment-751</link>
		<dc:creator>Binny V A</dc:creator>
		<pubDate>Thu, 14 May 2009 20:13:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=85#comment-751</guid>
		<description>I wrote about the different types of &lt;a href=&quot;http://www.openjs.com/articles/for_loop.php&quot; rel=&quot;nofollow&quot;&gt;For loops in JavaScript&lt;/a&gt; a while ago. I have also briefly touched on the optimization part of for loops.</description>
		<content:encoded><![CDATA[<p>I wrote about the different types of <a href="http://www.openjs.com/articles/for_loop.php" rel="nofollow">For loops in JavaScript</a> a while ago. I have also briefly touched on the optimization part of for loops.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stefano</title>
		<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/comment-page-1/#comment-750</link>
		<dc:creator>Stefano</dc:creator>
		<pubDate>Thu, 14 May 2009 09:59:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=85#comment-750</guid>
		<description>var items = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;];
for (var i = 0, l = items.length; i &lt; l; alert(items[i++]) );
// alert can be a lambda
// pay attention to l = items.length ... don&#039;t modify the length of the array inside the loop because it&#039;s read once before looping 

&quot;for in&quot; is not so safe...it loops on properties, so the array instance must be &quot;pure array&quot; to work correctly:

Array.prototype.bar=&quot;bar&quot;;
var items = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;];
items.foo=&#039;foo&#039;;

for (var p in items) {
    alert(items[p]);
};
//shows a, b, c, foo, bar</description>
		<content:encoded><![CDATA[<p>var items = ["a", "b", "c"];<br />
for (var i = 0, l = items.length; i &lt; l; alert(items[i++]) );<br />
// alert can be a lambda<br />
// pay attention to l = items.length &#8230; don&#8217;t modify the length of the array inside the loop because it&#8217;s read once before looping </p>
<p>&#8220;for in&#8221; is not so safe&#8230;it loops on properties, so the array instance must be &#8220;pure array&#8221; to work correctly:</p>
<p>Array.prototype.bar=&#8221;bar&#8221;;<br />
var items = ["a", "b", "c"];<br />
items.foo=&#8217;foo&#8217;;</p>
<p>for (var p in items) {<br />
    alert(items[p]);<br />
};<br />
//shows a, b, c, foo, bar</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tobias</title>
		<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/comment-page-1/#comment-749</link>
		<dc:creator>Tobias</dc:creator>
		<pubDate>Wed, 13 May 2009 10:10:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=85#comment-749</guid>
		<description>@John

Perhaps because it says: &quot;Warning: Never use a loop like this on arrays&quot; on https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Statements:for_each...in

The &#039;for (each) in&#039; loops over all the properties of an object. Not just the index of an array.</description>
		<content:encoded><![CDATA[<p>@John</p>
<p>Perhaps because it says: &#8220;Warning: Never use a loop like this on arrays&#8221; on <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Statements:for_each...in" rel="nofollow">https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Statements:for_each&#8230;in</a></p>
<p>The &#8216;for (each) in&#8217; loops over all the properties of an object. Not just the index of an array.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh McDonald</title>
		<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/comment-page-1/#comment-748</link>
		<dc:creator>Josh McDonald</dc:creator>
		<pubDate>Wed, 13 May 2009 07:07:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=85#comment-748</guid>
		<description>Why not:

for each (item in items) 
{
   alert(item);
}

Is any reasonable environment still stuck with JS &lt; 1.6?</description>
		<content:encoded><![CDATA[<p>Why not:</p>
<p>for each (item in items)<br />
{<br />
   alert(item);<br />
}</p>
<p>Is any reasonable environment still stuck with JS &lt; 1.6?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/comment-page-1/#comment-747</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Tue, 12 May 2009 17:28:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=85#comment-747</guid>
		<description>@Yes, indeed. But you can write something like that : &lt;em&gt;for (var i = 0, item; (item = items[i]) &#124;&#124; i &lt; items.length; i++)&lt;/em&gt;.

Things are getting out of control! ;)</description>
		<content:encoded><![CDATA[<p>@Yes, indeed. But you can write something like that : <em>for (var i = 0, item; (item = items[i]) || i < items.length; i++)</em>.</p>
<p>Things are getting out of control! <img src='http://www.javascriptkata.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rick</title>
		<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/comment-page-1/#comment-746</link>
		<dc:creator>Rick</dc:creator>
		<pubDate>Tue, 12 May 2009 17:26:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=85#comment-746</guid>
		<description>Stick with method #1.

#2 iterates over the array&#039;s properties, which are not necessarily only its values.  It should be avoided unless you have a good reason to do it this way.
#3 fails for the reason Robert pointed out, and also for sparse arrays.  For example:

var items = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;];
items[47] = &quot;d&quot;;

items.length will return 48, so method #1 will find [47] without trouble.  Method #3 will stop at [3], because it returns undefined.</description>
		<content:encoded><![CDATA[<p>Stick with method #1.</p>
<p>#2 iterates over the array&#8217;s properties, which are not necessarily only its values.  It should be avoided unless you have a good reason to do it this way.<br />
#3 fails for the reason Robert pointed out, and also for sparse arrays.  For example:</p>
<p>var items = ["a", "b", "c"];<br />
items[47] = &#8220;d&#8221;;</p>
<p>items.length will return 48, so method #1 will find [47] without trouble.  Method #3 will stop at [3], because it returns undefined.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Schultz</title>
		<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/comment-page-1/#comment-745</link>
		<dc:creator>Robert Schultz</dc:creator>
		<pubDate>Tue, 12 May 2009 16:45:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=85#comment-745</guid>
		<description>#3 fails when you have false or null as a value in the array:
var items = [&quot;a&quot;, &quot;b&quot;, false, &quot;c&quot;];

It&#039;ll stop before it gets to &quot;c&quot;

The first solution will correctly iterate over all of them.</description>
		<content:encoded><![CDATA[<p>#3 fails when you have false or null as a value in the array:<br />
var items = ["a", "b", false, "c"];</p>
<p>It&#8217;ll stop before it gets to &#8220;c&#8221;</p>
<p>The first solution will correctly iterate over all of them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2009/05/12/easy-loop-for-every-element-of-an-array/comment-page-1/#comment-744</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Tue, 12 May 2009 15:56:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.javascriptkata.com/?p=85#comment-744</guid>
		<description>@Rob
Sorry, my mistake. I edited the post.

@Femi
The variable is still manually assigned in the header of the loop, you can&#039;t avoid it. In this simple case, it would have been easier to simply write &lt;em&gt;alert(items[i])&lt;/em&gt; but when you have more code than that, it&#039;s confusing to always write it completly instead of its shorter version, &lt;em&gt;item&lt;/em&gt;.</description>
		<content:encoded><![CDATA[<p>@Rob<br />
Sorry, my mistake. I edited the post.</p>
<p>@Femi<br />
The variable is still manually assigned in the header of the loop, you can&#8217;t avoid it. In this simple case, it would have been easier to simply write <em>alert(items[i])</em> but when you have more code than that, it&#8217;s confusing to always write it completly instead of its shorter version, <em>item</em>.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
