Conditionals in JavaScript: Harvey Specter’s Guide to Making Decisions

Conditionals in JavaScript: Harvey Specter’s Guide to Making Decisions

Harvey is leaning back in his chair, Mike is pacing nervously, and Rachel is sitting across the desk, arms crossed.

Harvey: Mike, life is a series of decisions. And in JavaScript, just like in this firm, you make those decisions with conditionals. If you do this, this happens. Else, well… you’re screwed.

Mike: Okay, but what if I don’t know what to do?

Harvey: That’s why you have else if. But let’s start with the basics.


1. The if Statement: Harvey’s Ultimatum

Scenario: Harvey gives Mike a life-or-death decision.

Harvey: Mike, here’s the deal. If you tell Rachel the truth about your secret, you’ll be fired. Else, you’ll stay. It’s binary. Just like an if statement.

// Declare a constant variable 'tellTruth' and set it to false.
const tellTruth = false;  

// Check if 'tellTruth' is true
if (tellTruth) {  
  // If true, log that Mike is fired
  console.log("Mike is fired.");  
} else {  
  // If false, log that Mike stays at Pearson Specter Litt.
  console.log("Mike stays at Pearson Specter Litt.");  
}

// Output: Mike stays at Pearson Specter Litt.

Verdict:

  • If the condition (tellTruth) is true, the first block runs.

  • If it’s false, the else block runs.

Rachel (raising an eyebrow): So, Mike, what’s it gonna be?


2. The else if Ladder: Rachel’s Negotiation

Scenario: Rachel is negotiating a settlement and has multiple options.

Rachel: Harvey, it’s not always black and white. Sometimes, you need more options. That’s where else if comes in.

// Declare a constant variable 'settlementAmount' and set it to 1,000,000.
const settlementAmount = 1000000;  

// Check if the settlement amount is greater than 2,000,000
if (settlementAmount > 2000000) {  
  // If true, reject the offer and go to trial
  console.log("Reject the offer. We’re going to trial.");  
} 
// Otherwise, check if the settlement amount is at least 1,000,000
else if (settlementAmount >= 1000000) {  
  // If true, accept the offer as a fair deal
  console.log("Accept the offer. It’s a fair deal.");  
} 
// If neither condition is met, it means the offer is too low
else {  
  // Counter the offer since it's a lowball
  console.log("Counteroffer. They’re lowballing us.");  
}

// Output: Accept the offer. It’s a fair deal.

Verdict:

  • The else if ladder lets you handle multiple conditions in order.

  • The first condition that evaluates to true wins, and the rest are ignored.

Harvey (nodding): Not bad, Rachel. But what if I want to keep it simple?


3. Ternary Operator: Harvey’s One-Liner

Scenario: Harvey makes a quick decision on whether to take a case.

Harvey: Sometimes, you don’t need all the drama. Just a quick if-else in one line. That’s the ternary operator.

// Declare a constant variable 'caseIsStrong' and set it to true.
const caseIsStrong = true;  

// Use a ternary operator to decide whether to take the case or walk away.
const takeCase = caseIsStrong ? "Take the case." : "Walk away.";  

// Print the decision
console.log(takeCase);  

// Output: Take the case.

Verdict:

  • The ternary operator is perfect for quick decisions.

  • Syntax: condition ? expressionIfTrue : expressionIfFalse.

Mike (grinning): That’s so much cleaner than my nested if statements.

Harvey (smirking): That’s why I’m the closer.


4. Nested Conditionals: Louis’s Overcomplication

Scenario: Louis Litt overcomplicates a decision with nested if statements.

Louis: What if the client is unhappy? What if the judge rules against us? What if Donna finds out?

// Declare a constant variable 'clientIsHappy' and set it to true.
const clientIsHappy = true;  

// Declare a constant variable 'judgeRulesInFavor' and set it to false.
const judgeRulesInFavor = false;  

// Check if the client is happy
if (clientIsHappy) {  
  // If the client is happy, check if the judge ruled in our favor
  if (judgeRulesInFavor) {  
    // If true, we win the case
    console.log("Case closed. We win.");  
  } else {  
    // If false, appeal the decision
    console.log("Appeal the decision.");  
  }  
} else {  
  // If the client is not happy, prepare for a lawsuit
  console.log("Prepare for a lawsuit.");  
}

// Output: Appeal the decision.

Verdict:

  • Nested conditionals are powerful but can get messy.

  • Use them sparingly, or you’ll end up like Louis—overwhelmed and paranoid.

Donna (walking in): Louis, you’re overthinking this. Again.


5. Switch Statement: Jessica’s Strategic Move

Scenario: Jessica uses a switch statement to handle multiple outcomes in a merger deal.

Jessica: When you have a lot of conditions, switch is your best friend. It’s clean, efficient, and doesn’t waste time.

// Declare a constant variable 'mergerStatus' and set it to "approved".
const mergerStatus = "approved";  

// Use a switch statement to handle different merger statuses
switch (mergerStatus) {  
  case "approved":  
    // If the merger is approved, celebrate the decision
    console.log("Celebrate. We’re merging.");  
    break;  // Stops execution after this case

  case "pending":  
    // If the merger is pending, wait for the board’s decision
    console.log("Wait for the board’s decision.");  
    break;  

  case "rejected":  
    // If the merger is rejected, prepare for legal action
    console.log("Plan B. We’re suing.");  
    break;  

  default:  
    // If the status is unknown, investigate the situation
    console.log("Status unknown. Investigate.");  
}

// Output: Celebrate. We’re merging.

Verdict:

  • switch is ideal for multiple fixed outcomes.

  • Don’t forget the break statements, or you’ll fall through like a rookie.

Harvey (impressed): Jessica, you always know how to close the deal.


Closing Scene: Harvey’s Office

Harvey: Conditionals are like litigation. You weigh the options, make the call, and live with the consequences. But if you do it right, you’ll always come out on top.

Mike: So, what’s next? Loops?

Harvey: One case at a time, Mike. One case at a time.