Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre How JavaScript Reads Your Instructions | Understanding Program Flow and Decisions
Conditional Statements and Loops in JavaScript

bookHow JavaScript Reads Your Instructions

When you write a JavaScript program, the computer reads your instructions from top to bottom, one line at a time. This is called sequential execution. Each statement runs in the order you wrote it, unless you use special instructions to change that order. These special instructions are called control flow statements, and they include things like conditionals and loops.

Conditionals, such as if statements, tell the computer to make decisions and possibly skip some lines of code.

Loops, like for and while, allow the computer to repeat certain instructions multiple times.

By using these tools, you can make your programs smarter and more flexible, changing the path your code takes based on different situations.

1234567891011
console.log("Start"); console.log("Step 1"); console.log("Step 2"); let doExtraStep = true; if (doExtraStep) { console.log("Extra Step"); } console.log("Finish");
copy

In this example, JavaScript reads and runs each console.log in order. But when it reaches the if statement, it checks the condition. If doExtraStep is true, it runs the code inside the braces and prints "Extra Step". If doExtraStep were false, that line would be skipped.

This shows how you can change the flow of your program by using conditionals.

question-icon

Fill in the blank to complete an if statement that prints "Hello!" only if showGreeting is true.

let showGreeting = true; (showGreeting) { console.log("Hello!"); }
Hello!

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 2

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain how loops work in JavaScript?

What happens if I set `doExtraStep` to false?

Can you give more examples of control flow statements?

Awesome!

Completion rate improved to 7.69

bookHow JavaScript Reads Your Instructions

Glissez pour afficher le menu

When you write a JavaScript program, the computer reads your instructions from top to bottom, one line at a time. This is called sequential execution. Each statement runs in the order you wrote it, unless you use special instructions to change that order. These special instructions are called control flow statements, and they include things like conditionals and loops.

Conditionals, such as if statements, tell the computer to make decisions and possibly skip some lines of code.

Loops, like for and while, allow the computer to repeat certain instructions multiple times.

By using these tools, you can make your programs smarter and more flexible, changing the path your code takes based on different situations.

1234567891011
console.log("Start"); console.log("Step 1"); console.log("Step 2"); let doExtraStep = true; if (doExtraStep) { console.log("Extra Step"); } console.log("Finish");
copy

In this example, JavaScript reads and runs each console.log in order. But when it reaches the if statement, it checks the condition. If doExtraStep is true, it runs the code inside the braces and prints "Extra Step". If doExtraStep were false, that line would be skipped.

This shows how you can change the flow of your program by using conditionals.

question-icon

Fill in the blank to complete an if statement that prints "Hello!" only if showGreeting is true.

let showGreeting = true; (showGreeting) { console.log("Hello!"); }
Hello!

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 2
some-alt