for in example in javascript in javascript

In JavaScript, the syntax of the for loop is as follows:

index.tsx
for (initialization; condition; increment/decrement) {
  // code block to be executed
}
88 chars
4 lines

Here's an example of using for loop in JavaScript to loop through an array:

index.tsx
var fruits = ["Apple", "Banana", "Orange"];

for (var i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}
115 chars
6 lines

This will output:

index.tsx
Apple
Banana
Orange
20 chars
4 lines

In the above example, i is the index variable that is initialized to 0 and it increments by 1 on each iteration until it is less than the length of the array (fruits.length). The console.log statement inside the loop is executed for each value of i, which prints the corresponding element of the array to the console.

related categories

gistlibby LogSnag