Introduction
An if-else statement helps your program decide what to do based on a condition. It lets your code choose between two paths.
An if-else statement helps your program decide what to do based on a condition. It lets your code choose between two paths.
if (condition) { // code to run if condition is true } else { // code to run if condition is false }
condition is a question your program asks that can be true or false.else runs.age is 18 or more. It prints a message based on that.if (age >= 18) { console.log('You can vote'); } else { console.log('You are too young to vote'); }
if (temperature > 30) { console.log('It is hot outside'); } else { console.log('It is not hot outside'); }
This program checks if a number is even or odd by using the remainder operator %. If the remainder when dividing by 2 is zero, it is even; otherwise, odd.
const number = 10; if (number % 2 === 0) { console.log('The number is even'); } else { console.log('The number is odd'); }
Always use curly braces {} to keep your code clear and avoid mistakes.
The condition inside if must result in true or false.
If-else lets your program choose between two actions.
The if block runs when the condition is true.
The else block runs when the condition is false.
if-else statement do in JavaScript?let age = 18;
if (age >= 18) {
console.log('Adult');
} else {
console.log('Minor');
}let score = 75;
if score >= 60 {
console.log('Pass');
} else {
console.log('Fail');
}let num = 7; // Choose the correct code snippet
Pass the test and this pattern moves unknown