Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda for Loops | Building Conditions and Repetition
Conditional Statements and Loops in JavaScript

bookfor Loops

Imagine you need to take 10 steps forward, one at a time, carefully counting each step as you go. Instead of telling yourself "take a step" ten separate times, you could set up a simple rule: "Start at 1, and for each number up to 10, take a step and count." This is how a for loop works in JavaScript—it lets you repeat an action a certain number of times, just like counting out your steps.

1234
// Log numbers 1 to 10 in the console using a for loop for (let i = 1; i <= 10; i++) { console.log(i); }
copy

In this for loop, you see three main parts inside the parentheses: let i = 1 starts the count at 1, i <= 10 tells the loop to keep going as long as i is 10 or less, and i++ increases the count by 1 each time. The loop repeats the action in its block—printing the current number—until the condition is no longer true.

1. Which part of the for loop below controls how many times the loop runs?

2. Fill in the missing part of this for loop so it logs the numbers 1 to 5 in the console. Type the condition that should go in place of the comment so the loop counts from 1 to 5.

question mark

Which part of the for loop below controls how many times the loop runs?

Select the correct answer

question-icon

Fill in the missing part of this for loop so it logs the numbers 1 to 5 in the console. Type the condition that should go in place of the comment so the loop counts from 1 to 5.

for (let i = 1; ; i++) { console.log(i); }
1
2
3
4
5

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain what would happen if I changed the starting value of i?

What does i++ mean in this context?

How can I make the loop count backwards from 10 to 1?

Awesome!

Completion rate improved to 7.69

bookfor Loops

Deslize para mostrar o menu

Imagine you need to take 10 steps forward, one at a time, carefully counting each step as you go. Instead of telling yourself "take a step" ten separate times, you could set up a simple rule: "Start at 1, and for each number up to 10, take a step and count." This is how a for loop works in JavaScript—it lets you repeat an action a certain number of times, just like counting out your steps.

1234
// Log numbers 1 to 10 in the console using a for loop for (let i = 1; i <= 10; i++) { console.log(i); }
copy

In this for loop, you see three main parts inside the parentheses: let i = 1 starts the count at 1, i <= 10 tells the loop to keep going as long as i is 10 or less, and i++ increases the count by 1 each time. The loop repeats the action in its block—printing the current number—until the condition is no longer true.

1. Which part of the for loop below controls how many times the loop runs?

2. Fill in the missing part of this for loop so it logs the numbers 1 to 5 in the console. Type the condition that should go in place of the comment so the loop counts from 1 to 5.

question mark

Which part of the for loop below controls how many times the loop runs?

Select the correct answer

question-icon

Fill in the missing part of this for loop so it logs the numbers 1 to 5 in the console. Type the condition that should go in place of the comment so the loop counts from 1 to 5.

for (let i = 1; ; i++) { console.log(i); }
1
2
3
4
5

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3
some-alt