Loops let you repeat a block of code. JavaScript offers several loop styles.
for loopfor (let i = 0; i < 5; i++) {
console.log(i); // 0 1 2 3 4
}
Iterating an array:
const fruits = ["apple", "banana", "cherry"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
for...of — iterate valuesCleaner syntax when you only need the value, not the index:
for (const fruit of fruits) {
console.log(fruit);
}
for...in — iterate object keysconst person = { name: "Alice", age: 30 };
for (const key in person) {
console.log(key, person[key]); // name Alice / age 30
}
sumArray(numbers) that returns the sum of all numbers in the array using a for loop.reverseArray(arr) that returns a new array with the elements in reverse order.sumArray([1, 2, 3, 4, 5]) and display in #output as "Sum: 15".A standard for loop: for (let i = 0; i < arr.length; i++).