⚡
Conditional Statements
Beginner
70 XP
25 min
Lesson Content
Conditional Statements
Conditional statements allow your code to make decisions and execute different code based on conditions. They're essential for creating interactive and dynamic programs.
If Statement
if (condition) {
// code runs if condition is true
}If-Else
if (condition) {
// code if true
} else {
// code if false
}If-Else If-Else
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else {
grade = 'F';
}Ternary Operator
let result = condition ? valueIfTrue : valueIfFalse;
let status = age >= 18 ? 'Adult' : 'Minor';Example Code
Create a program that assigns grades based on score
let score = 82;
let grade;
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
console.log('Score:', score);
console.log('Grade:', grade);Expected Output:
Score: 82 Grade: B
Study Tips
- •Read the theory content thoroughly before practicing
- •Review the example code to understand key concepts
- •Proceed to the Practice tab when you're ready to code