JavaScript
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
23: Conditional Statements
A few years ago, I was reviewing a pull request from a junior developer who was building a checkout system for an e-commerce site. He had written a great piece of logic to apply a 10% discount for first-time buyers. It worked perfectly during his tests. However, when we hit production, we realized that users who had a "VIP" status were suddenly losing their 20% discount because the code stopped checking for VIP status the moment it saw they were also first-time buyers. He had the logic in the right place, but the order and structure of his conditional statements were fighting each other. It was a humbling reminder that in programming, the path the code takes is just as important as the destination.
Diverting the Flow with If/Else
At its core, a conditional statement is just a fork in the road. You're telling JavaScript: "If this specific thing is true, do this; otherwise, do that." We use the if statement to wrap a block of code that should only execute if a condition evaluates to true. If that condition is false, the engine simply skips over that block entirely.
const userBalance = 45;
const itemPrice = 50;
if (userBalance >= itemPrice) {
console.log("Purchase successful!");
} else {
console.log("Insufficient funds. Please add more credits.");
}
In the example above, the expression userBalance >= itemPrice is evaluated first. Since 45 is not greater than or equal to 50, the if block is ignored and the else block runs. I usually tell people to think of the else block as your safety net—it's what happens when none of your specific requirements are met.
Stacking Conditions with Else If
Real-world logic is rarely a simple "yes or no." Most of the time, you have a handful of different possibilities. This is where else if comes in. It allows you to chain multiple checks together. The critical thing to remember here—and this is where that junior dev I mentioned tripped up—is that JavaScript executes these in order. Once it finds a condition that is true, it runs that block and exits the entire chain, ignoring everything else below it.
const userRole = 'editor';
if (userRole === 'admin') {
console.log("Full access: You can edit, delete, and manage users.");
} else if (userRole === 'editor') {
console.log("Limited access: You can edit and publish posts.");
} else if (userRole === 'viewer') {
console.log("Read-only access: You can only view posts.");
} else {
console.log("Guest access: Please log in to see more.");
}
If userRole was 'admin', the first block would run, and JavaScript wouldn't even look at the 'editor' or 'viewer' checks. If you have conditions that overlap, always put the most specific or most restrictive condition at the top of the chain.
The Ternary Shortcut
Sometimes, writing a full if/else block feels like overkill, especially when you're just trying to assign a value to a variable based on a condition. In those cases, I use the ternary operator. It's essentially a one-line if/else statement. The syntax is a bit strange at first—a question mark followed by a colon—but it's incredibly efficient once you get used to it.
const isLoggedIn = true;
const welcomeMessage = isLoggedIn ? "Welcome back, user!" : "Please sign in to continue.";
console.log(welcomeMessage);
The logic is: condition ? expressionIfTrue : expressionIfFalse. I love using these for UI state toggles, but a word of caution: don't nest ternaries inside other ternaries. It makes the code nearly impossible to read, and your future self (or your teammates) will definitely hate you for it.
📋 Practical Task
Build a Dynamic Subscription-Based Feature Gate
You are building a feature-gate system for a SaaS application. Your goal is to determine what level of access a user has based on their subscription tier and whether their account is currently active.
- Create a variable
subscriptionTier(set it to either 'free', 'pro', or 'enterprise'). - Create a boolean variable
isActive(set it totrueorfalse). - Write a conditional structure that logs the following messages to the console:
- If
isActiveisfalse, log: "Account deactivated. Please contact support." (This should take priority over any tier check). - If
isActiveistrueAND the tier is 'enterprise', log: "Welcome, Enterprise user! You have unlimited access and a dedicated account manager." - If
isActiveistrueAND the tier is 'pro', log: "Welcome, Pro user! You have access to all premium features." - If
isActiveistrueAND the tier is 'free', log: "Welcome, Free user! Upgrade to Pro for more features." - For any other case (e.g., an unrecognized tier), log: "Invalid account status."
- If
Test your code by changing the values of subscriptionTier and isActive to ensure every path works correctly.
There are no comments for now.