Javascript For Loops
In this tutorial we'll start with introduction to Loops and then cover For Loops in JS.
Loops allow you to execute a piece of code repeatedly as long as the specified condition holds true!
Remember, if the specified condition never evaluates to false
then it'll lead to an infinite loop that'll never end causing your browser (or server) to slow down and maybe even crash.
An example of infinite for loop:
for (var i = 0; i < 10;);
The loop has no statement inside. Since there's no incrementExpression , i
will always be 0, and the condition i < 10
will always evaluate to true
causing the loop to execute endlessly.
Notes:
- In the codecast, this piece of code is used -
Array(j).join('*')
. It basically repeats a piece of stringj-1
times. So ifj
is 6, then the expression will return '*****' (5 times).
Related:
Warning: Invalid argument supplied for foreach() in /var/www/cssdeck.com/app/modules/labs/views/_details.php on line 68