EN
JavaScript - if...else statement
0 points
In this article, we would like to show you how to use if...else statement in JavaScript.
The if
statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement can be executed using else if
or else
keyword.
xxxxxxxxxx
1
if (condition) {
2
// statement 1
3
} else {
4
// statement 2
5
}
xxxxxxxxxx
1
let x = 1;
2
3
if (x == 1) {
4
console.log('x is equal to 1');
5
} else {
6
console.log('x is not equal to 1');
7
}
Output:
xxxxxxxxxx
1
x is equal to 1
xxxxxxxxxx
1
let x = 1;
2
3
if (x > 0) {
4
console.log('x is a positive number');
5
} else if (x < 0) {
6
console.log('x is a negative number');
7
} else {
8
console.log('x is equal to 0');
9
}
Output:
xxxxxxxxxx
1
x is a positive number