DEV Community

Cover image for ๐Ÿง  JavaScript Conditional Statements Explained for Beginners
NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on

๐Ÿง  JavaScript Conditional Statements Explained for Beginners

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");
}
Enter fullscreen mode Exit fullscreen mode

2. if...else Statement

let age = 16;
if (age >= 18) {
  console.log("You're an adult");
} else {
  console.log("You're underage");
}
Enter fullscreen mode Exit fullscreen mode

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");
}
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช 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.");
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“˜ 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


๐Ÿ”– Tags

#JavaScript #WebDevelopment #CodingForBeginners #Frontend #Programming #LearnToCode


Top comments (0)