It is used to loop though object like array.
it will execute the loop till the end of array or list of item.
Syntax:
1 2 3 |
for (variableName in object) { // statement or block to execute } |
Example Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <p id="div1"></p> <script type="text/javascript"> const fruits = ["apple", "banana", "orange", "guava"]; let text = ""; for (let i in fruits) { text += fruits[i] + "<br>"; } document.getElementById("div1").innerHTML = text; </script> </body> </html> |
Output:
apple
banana
orange
guava