A JavaScript for loop repeats a block of code a specific number of times when the number of iterations is known, utilizing three expressions: initialization, condition, and increment (or afterthought). The standard syntax is for (initialization; condition; afterthought) { // code block }, where the initialization runs once, the condition is checked before each iteration, and the afterthought executes after every loop pass.
for (let i = 0; i < 5; i++) {
console.log("The number is " + i);
}Other iteration methods in JavaScript include:
while: Executes code as long as a specified condition is true.
do...while: Similar to while but guarantees the code block runs at least once before checking the condition.
for...in: Iterates over the enumerable properties of an object.
for...of: Iterates over the values of iterable objects like arrays, strings, Maps, and Sets.
forEach(): A method that iterates over each element in an array using a callback function.