JS: for loop | JavaScript
JavaScript for loop
The for loop iterates the block of statments, expecting three optional statements seperated by semicolon (;)
Syntax
for(intialization(s); condition(s); increment(s)){
//body
}
for(var i = 0; i < 10; i++){
//body
}
All the three arguments are the JavaScript valid statements;
initialization var i = 0
new variable(s) can be intialized as well as reassign a value, seperated by comma.
condition i
for loop checks the condition for each iteration. One or more conditions can be given. All condition must be true.
increment i++
one or more variables values can be changed, increment or decrement allowed.
Terminate for-loop
A for loop can be terminated with break keyword.
for(i = 1; i <= 100 ;i++)
if(i > 10)
break;
else
console.log(5, 'x', i, '=', 5 * i);
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
The program prints table of 5, from 1 through 10. At 11th iteration, loop will be terminated with break keyword to exit the loop.
Jump to next iteration
A for loop may call its next iteration in the loop-block using continue
keyword:
Following program prints even numbers only.
for(i = 1; i <= 10 ;i++)
if(i % 2 == 1) //i.e. odd number
continue; //next iteration
else
console.log(i); //print the even number
2
4
6
6
8
10
for
in
and for
of
Differenece
consider an array of fruits
var arr = ['Apple', 'Banana', 'Cherry'];
Following are two diffrent output
for(var o in arr)
console.log(o);
0
1
2
for(var o of arr)
console.log(o);
Apple
Banana
Cherry
for
of
for-of works with iterable variables and expressions only. for-of iterates the elements of an array.
for
in
for-in iterates the properties of an object and index of an array.
- JavaScript
for
-loop consists of three parts seperated by semicolon.
- All three parts are optional
- for-of requires iterable types only
- for-of iterates the elements of the given array
- for-in iterates the 0-based index of the given array
- for-in iterates the properties of the given object
for loop checks the condition for each iteration. One or more conditions can be given. All condition must be true.
increment i++
one or more variables values can be changed, increment or decrement allowed.
Terminate for-loop
A for loop can be terminated with break keyword.
for(i = 1; i <= 100 ;i++)
if(i > 10)
break;
else
console.log(5, 'x', i, '=', 5 * i);
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
The program prints table of 5, from 1 through 10. At 11th iteration, loop will be terminated with break keyword to exit the loop.
Jump to next iteration
A for loop may call its next iteration in the loop-block using continue
keyword:
Following program prints even numbers only.
for(i = 1; i <= 10 ;i++)
if(i % 2 == 1) //i.e. odd number
continue; //next iteration
else
console.log(i); //print the even number
2
4
6
6
8
10
for
in
and for
of
Differenece
consider an array of fruits
var arr = ['Apple', 'Banana', 'Cherry'];
Following are two diffrent output
for(var o in arr)
console.log(o);
0
1
2
for(var o of arr)
console.log(o);
Apple
Banana
Cherry
for
of
for-of works with iterable variables and expressions only. for-of iterates the elements of an array.
for
in
for-in iterates the properties of an object and index of an array.
- JavaScript
for
-loop consists of three parts seperated by semicolon. - All three parts are optional
- for-of requires iterable types only
- for-of iterates the elements of the given array
- for-in iterates the 0-based index of the given array
- for-in iterates the properties of the given object