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

Contenu du cours

Introduction to JavaScript

Introduction to JavaScript

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

book
The `if` Statement

Note
Definition

A conditional statement, also known as an if-statement, can be used to execute specific lines of code based on the result of a boolean expression.

The syntax of a conditional statement is:

js

Here boolean_expression can be any expression which defines a condition. The code between the curly brackets ({}) is executed if the boolean_expression evaluates to true.

Note
Note

The code enclosed between the curly brackets ({}) is collectively referred to as a Code Block.

For example:

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

We can have multiple if-statements in a program, giving us more control over which code block to execute. For example, we can further extend the previous code to output a different statement if the age is less than 18:

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

1. What is a conditional statement used for in JavaScript?

2. What is the correct syntax for an if statement?

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

question mark

What is a conditional statement used for in JavaScript?

Select the correct answer

question mark

What is the correct syntax for an if statement?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 3

Demandez à l'IA

expand
ChatGPT

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

course content

Contenu du cours

Introduction to JavaScript

Introduction to JavaScript

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

book
The `if` Statement

Note
Definition

A conditional statement, also known as an if-statement, can be used to execute specific lines of code based on the result of a boolean expression.

The syntax of a conditional statement is:

js

Here boolean_expression can be any expression which defines a condition. The code between the curly brackets ({}) is executed if the boolean_expression evaluates to true.

Note
Note

The code enclosed between the curly brackets ({}) is collectively referred to as a Code Block.

For example:

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

We can have multiple if-statements in a program, giving us more control over which code block to execute. For example, we can further extend the previous code to output a different statement if the age is less than 18:

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

1. What is a conditional statement used for in JavaScript?

2. What is the correct syntax for an if statement?

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

question mark

What is a conditional statement used for in JavaScript?

Select the correct answer

question mark

What is the correct syntax for an if statement?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 3
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt