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
- πΌ LinkedIn
- π My Portfolio
π Tags
#JavaScript
#WebDevelopment
#CodingForBeginners
#Frontend
#Programming
#LearnToCode
Top comments (0)