<p>How can I build a loop in JavaScript?</p>

[{'answer_id': 52083, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': -1, 'selected': False, 'text': '<p>A loop in JavaScript looks like this:</p>\n\n<pre><code>for (var = startvalue; var &lt;= endvalue; var = var + increment) {\n    // code to be executed\n}\n</code></pre>\n'}, {'answer_id': 52088, 'author': 'minty', 'author_id': 4491, 'author_profile': 'https://Stackoverflow.com/users/4491', 'pm_score': 1, 'selected': False, 'text': '<p>Here is an example of a for loop:</p>\n\n<p>We have an array of items <strong>nodes</strong>.</p>\n\n<pre><code>for(var i = 0; i&lt; nodes.length; i++){\n    var node = nodes[i];\n    alert(node);\n}\n</code></pre>\n'}, {'answer_id': 52101, 'author': 'georgebrock', 'author_id': 5168, 'author_profile': 'https://Stackoverflow.com/users/5168', 'pm_score': 6, 'selected': True, 'text': '<p><strong>For loops</strong></p>\n\n<pre><code>for (i = startValue; i &lt;= endValue; i++) {\n    // Before the loop: i is set to startValue\n    // After each iteration of the loop: i++ is executed\n    // The loop continues as long as i &lt;= endValue is true\n}\n</code></pre>\n\n<p><strong>For...in loops</strong></p>\n\n<pre><code>for (i in things) {\n    // If things is an array, i will usually contain the array keys *not advised*\n    // If things is an object, i will contain the member names\n    // Either way, access values using: things[i]\n}\n</code></pre>\n\n<p>It is bad practice to use <code>for...in</code> loops to itterate over arrays. It goes against the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" rel="noreferrer">ECMA 262</a> standard and can cause problems when non-standard attributes or methods are added to the Array object, e.g. by <a href="http://www.prototypejs.org/api/array" rel="noreferrer">Prototype</a>.\n<em>(Thanks to <a href="https://stackoverflow.com/users/7679/chase-seibert">Chase Seibert</a> for pointing this out in the comments)</em></p>\n\n<p><strong>While loops</strong></p>\n\n<pre><code>while (myCondition) {\n    // The loop will continue until myCondition is false\n}\n</code></pre>\n'}, {'answer_id': 32355895, 'author': 'Nina Scholz', 'author_id': 1447675, 'author_profile': 'https://Stackoverflow.com/users/1447675', 'pm_score': 0, 'selected': False, 'text': '<p>Aside form the build-in loops (<code>while() ...</code>, <code>do ... while()</code>, <code>for() ...</code>), there is a structure of self calling function, also known as <strong>recursion</strong> to create a loop without the three build-in loop structures.</p>\n\n<p>Consider the following:</p>\n\n<p><div class="snippet" data-lang="js" data-hide="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>// set the initial value\r\nvar loopCounter = 3;\r\n\r\n// the body of the loop\r\nfunction loop() {\r\n\r\n    // this is only to show something, done in the loop\r\n    document.write(loopCounter + \'&lt;br&gt;\');\r\n\r\n    // decrease the loopCounter, to prevent running forever\r\n    loopCounter--;\r\n\r\n    // test loopCounter and if truthy call loop() again \r\n    loopCounter &amp;&amp; loop();\r\n}\r\n\r\n// invoke the loop\r\nloop();</code></pre>\r\n</div>\r\n</div>\r\n</p>\n\n<p>Needless to say that this structure is often used in combination with a return value, so this is a small example how to deal with value that is not in the first time available, but at the end of the recursion:</p>\n\n<p><div class="snippet" data-lang="js" data-hide="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>function f(n) {\r\n    // return values for 3 to 1\r\n    //       n   -n  ~-n   !~-n   +!~-n   return\r\n    // conv int neg bitnot  not  number \r\n    //       3   -3   2    false    0    3 * f(2)\r\n    //       2   -2   1    false    0    2 * f(1)\r\n    //       1   -1   0     true    1        1\r\n    // so it takes a positive integer and do some conversion like changed sign, apply\r\n    // bitwise not, do logical not and cast it to number. if this value is then\r\n    // truthy, then return the value. if not, then return the product of the given\r\n    // value and the return value of the call with the decreased number\r\n    return +!~-n || n * f(n - 1);\r\n}\r\n\r\ndocument.write(f(7));</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}]
['https://Stackoverflow.com/questions/52080', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/115/']