Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele 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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 7.69

bookHow JavaScript Reads Your Instructions

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 2
some-alt