For Loops

Automate repetitive tasks by telling JavaScript to repeat code a specific number of times.

Step 1 of 5

Why do we need loops?

Imagine you run an online store and need to display 100 products on a page. Would you write `console.log(product1)`, `console.log(product2)`, ... 100 times? Of course not! That would be tedious, error-prone, and impossible to maintain.

Loops let you say: 'Repeat this block of code a certain number of times' or 'Repeat this for every item in a list.' They're one of the most powerful tools in programming because they turn 100 lines of repetitive code into 3.

Every website you use relies on loops. When Twitter displays your timeline, it loops through an array of tweets and renders each one. When Spotify shows your playlist, it loops through the song list. When Google shows search results, it loops through the results array.

Think of it this way: A for loop is like a gym workout with reps: 'Start at rep 1, keep going while under 10, add 1 after each rep.' You define the starting point, the stopping condition, and what changes after each repetition.
Web Standard
The for loop syntax in JavaScript is identical to C, Java, and many other languages. The three-part header (initialization; condition; update) is one of the most universal patterns in programming — learn it once, use it everywhere.
Learn more on MDN