JavaScript iterable statements are used to iterate a piece of code using while, while or for-in loops. It is mostly used to access the value in array. We use iterable statement because, if a line of code has to be executed multiple times, then we put that statement in the loop. by which it runs automatically
In JavaScript has five types of iterable statement.
- for loop
- while loop
- do-while loop
- for-in loop
- for-of loop
Syntax:
for-loop
for(variable declaration; condition ; updation)
statement
while loop
variable declaration;
while(condition){
statement;
updation;
}
do-while loop
variable declaration;
do{
statement;
updation;
}while(condition);
for-in loop
for( variable in objects)
statement;
for-of loop
for (variable of iterable) {
statement
}
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript For Loop</h2>
<p id='demo'></p>
<script>
const fruits = ['Apple', 'Banana', 'Graps', 'Kiwi', 'Mango', 'Orange'];
let text = '';
for (let i = 0; i < fruits.length; i++) {
text += fruits[i] + '<br>';
}
document.getElementById('demo').innerHTML = text;
</script>
</body>
</html>