<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Jaiprakash : for in loop</title><link>http://blogs.msdn.com/jaiprakash/archive/tags/for+in+loop/default.aspx</link><description>Tags: for in loop</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>For In Loop in JScript</title><link>http://blogs.msdn.com/jaiprakash/archive/2006/12/28/for-in-loop-in-jscript.aspx</link><pubDate>Thu, 28 Dec 2006 09:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1373255</guid><dc:creator>Jaiprakash</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jaiprakash/comments/1373255.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jaiprakash/commentrss.aspx?PostID=1373255</wfw:commentRss><description>&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Hi,&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;It's been more than one year since my last post. I will try to be regular now on :). &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;BTW, during this period, I have moved to Jscript team and will be sharing my little knowledge on&amp;nbsp;JScript with you all. &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Recently I investigated a bug wherein user complained that for... In loop in JScript doesn't work. It doesn't enumerate through members of an array like the for..each loop of VBScript or C#. Since I am also new to Jscript and was not sure of the for...in behavior I decided to debug the code and here are the results...&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;JScript For in loop is different from for-each of VBScript or C#. For each loop of VBScript and C# indeed iterates through the members of an object but Jscript for in loop actually iterates through indexes. What does that mean? Iterating through indexes not members? Well, let's work out an example...&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;var arr = new Array(10, 20, 30);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;for (oBinding in arr)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;WScript.Echo(oBinding)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;What do you think should be the output of above code snippet? Shouldn't it be 10, 20, 30? &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;If you run above code, then output will be 0, 1, 2, which are actually array indices. If you want to print the members then you need to change above code as following...&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;var arr = new Array(10, 20, 30);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;for (oBinding in arr)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; TEXT-INDENT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;WScript.Echo(&lt;STRONG&gt;&lt;SPAN style="FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;arr[&lt;/SPAN&gt;&lt;/STRONG&gt;oBinding&lt;STRONG&gt;&lt;SPAN style="FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;]&lt;/SPAN&gt;&lt;/STRONG&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Above code snippet prints what you were expecting, 10, 20, 30. Hmmmm.... then why do i need a for..in loop at all.&amp;nbsp;I can just use a for loop to print the array members like following....&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; LINE-HEIGHT: 150%"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;for (count = 0; count &amp;lt; arr.length; count++)&lt;BR&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;WScript.Echo(arr[count]);&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Correct! But the need of for..in loop arises when you have a sparse array.&amp;nbsp;JScript arrays are sparse arrays, that is, although you can allocate an array with many elements, only the elements that actually contain data exist. This reduces the amount of memory used by the array. Consider following example...&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; LINE-HEIGHT: 150%"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;var arr = new Array();&lt;BR&gt;arr[10] = 2;&lt;BR&gt;arr[15] = 4;&lt;BR&gt;arr[20] = 5;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; LINE-HEIGHT: 150%"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;for (oBinding in arr)&lt;BR&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; LINE-HEIGHT: 150%"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WScript.Echo(arr[oBinding])&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN-LEFT: 0.5in; LINE-HEIGHT: 150%"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Verdana; mso-bidi-font-family: Arial"&gt;}&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 150%; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;In&amp;nbsp;above code array arr has only 3 valid elements though it's size is 20. If you use a for loop to access this array, you will be accessing all 20 elements which is unnecessay waste of CPU time. Instead if you use for...in loop you will just access 3 elements. Huge saving of CPU!!!&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;In other words what you can say is that for..in allows you to randomly access the array. &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;Same holds true for object properties also. What if you have a Jscript object but you don't know what all properties it holds. Answer is the for…in loop. Just use a for..in loop on that object and get hold of all the properties. Simple!&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1373255" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jaiprakash/archive/tags/for+in+loop/default.aspx">for in loop</category><category domain="http://blogs.msdn.com/jaiprakash/archive/tags/JScript/default.aspx">JScript</category></item></channel></rss>