Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele The `else` Clause | Conditional Statements
Introduction to JavaScript
course content

Kurssisisältö

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
The `else` Clause

Conditional Statements have an optional else clause which can be used to specify a block of code to be executed in case the boolean expression evaluates to false.

The syntax is as follows:

js

This way we don't need to write multiple if-statements to check for opposite conditions, making the code shorter and neater.

1234567
let age = 18; if(age >= 18) { console.log("The user is old enough to drive."); } else { console.log("The user is not old enough to drive"); }
copy

In the code above, we simply used an else block instead of rewriting a separate if-statement.

1. What is the purpose of the else clause in an if-else statement?

2. What will be the output of the following code?

3. What happens if the if condition evaluates to true?

question mark

What is the purpose of the else clause in an if-else statement?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

question mark

What happens if the if condition evaluates to true?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 7

Kysy tekoälyä

expand
ChatGPT

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

course content

Kurssisisältö

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
The `else` Clause

Conditional Statements have an optional else clause which can be used to specify a block of code to be executed in case the boolean expression evaluates to false.

The syntax is as follows:

js

This way we don't need to write multiple if-statements to check for opposite conditions, making the code shorter and neater.

1234567
let age = 18; if(age >= 18) { console.log("The user is old enough to drive."); } else { console.log("The user is not old enough to drive"); }
copy

In the code above, we simply used an else block instead of rewriting a separate if-statement.

1. What is the purpose of the else clause in an if-else statement?

2. What will be the output of the following code?

3. What happens if the if condition evaluates to true?

question mark

What is the purpose of the else clause in an if-else statement?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

question mark

What happens if the if condition evaluates to true?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 7
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt