if statement
It's our turn to control the code; now we will set conditions for its execution!
Let's consider the most basic form of conditional statements - the if
statement.
Let's move on to the syntax:
if (condition) {
// code to be executed if the condition is true
}
For example:
12345let amIHappy = true; if (amIHappy) { console.log('I am so happy!'); } console.log("It's Monday");
That's almost all there is to it, but there's one more thing worth mentioning. Inside the condition
block, there should be a boolean
value. However, we can also place an expression that will result in a boolean
value inside this block, for example:
1234let age: number = 22; if (age >= 21) { console.log('You can buy alcohol'); }
You can change the value of the variable age
to see how the code works depending on that value.
I'll tell you how to set a condition if the first one doesn't work in the next chapter!
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you explain more about boolean expressions in if statements?
What happens if the condition in the if statement is false?
Can you show more examples of if statements with different conditions?
Awesome!
Completion rate improved to 2.94
if statement
Scorri per mostrare il menu
It's our turn to control the code; now we will set conditions for its execution!
Let's consider the most basic form of conditional statements - the if
statement.
Let's move on to the syntax:
if (condition) {
// code to be executed if the condition is true
}
For example:
12345let amIHappy = true; if (amIHappy) { console.log('I am so happy!'); } console.log("It's Monday");
That's almost all there is to it, but there's one more thing worth mentioning. Inside the condition
block, there should be a boolean
value. However, we can also place an expression that will result in a boolean
value inside this block, for example:
1234let age: number = 22; if (age >= 21) { console.log('You can buy alcohol'); }
You can change the value of the variable age
to see how the code works depending on that value.
I'll tell you how to set a condition if the first one doesn't work in the next chapter!
Grazie per i tuoi commenti!