If you're just getting started with JavaScript, understanding conditional statements is crucial for writing logic-driven code. In this post, we’ll break down if
, else
, else if
, and the ternary operator in the simplest way possible.
📺 Watch the full tutorial here:
✨ What Are Conditional Statements?
Conditional statements allow your program to make decisions based on certain conditions. Think of them as ways to say:
“If this happens, do that. Otherwise, do something else.”
✅ Basic Syntax
1. if
Statement
let age = 18;
if (age >= 18) {
console.log("You're an adult");
}
2. if...else
Statement
let age = 16;
if (age >= 18) {
console.log("You're an adult");
} else {
console.log("You're underage");
}
3. if...else if...else
let score = 75;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B");
} else if (score >= 70) {
console.log("C");
} else {
console.log("F");
}
4. Ternary Operator
A shorthand way of writing an if...else
:
let isMember = true;
let fee = isMember ? "$2.00" : "$10.00";
console.log(fee); // "$2.00"
🧪 Real-Life Use Case Example
Imagine you're building a login system:
const isLoggedIn = true;
if (isLoggedIn) {
console.log("Welcome back!");
} else {
console.log("Please log in.");
}
📘 What You'll Learn in the Video
✅ How if
statements work
✅ When to use else
and else if
✅ How to simplify logic using ternary operators
✅ Real-world use cases with clear code examples
🔗 Watch the Full Video
▶️ JavaScript Conditional Statements for Beginners
If you found this helpful, consider subscribing to my YouTube channel for more beginner-friendly JavaScript tutorials and dev content.
📩 Stay Connected
- 🌐 My Portfolio
🔖 Tags
#JavaScript
#WebDevelopment
#CodingForBeginners
#Frontend
#Programming
#LearnToCode