Week 2
Working with Data

Conditional Statements

Learn how to make decisions in your code with conditional statements!

Learning Objectives
  • Master if, else if, and else statements
  • Understand switch statements
  • Learn conditional (ternary) operators

Explanation

Conditional statements allow your code to make decisions based on certain conditions. They help your program take different paths depending on different situations. 1. if statement: The basic form of a conditional. If the condition is true, the code inside the block runs. 2. if...else statement: Adds an alternative block of code that runs when the condition is false. 3. if...else if...else statement: Allows checking multiple conditions in sequence. 4. switch statement: An alternative to multiple if...else if statements when checking the same variable against different values. 5. Conditional (ternary) operator: A shorthand way to write simple if...else statements: condition ? valueIfTrue : valueIfFalse When working with conditionals, it's important to understand "truthy" and "falsy" values in JavaScript: - Falsy values: false, 0, "", null, undefined, NaN - Truthy values: Everything else
Code Example
// Basic if statement
let age = 16;
if (age >= 18) {
  console.log("You are an adult!");
}

// if...else statement
if (age >= 18) {
  console.log("You are an adult!");
} else {
  console.log("You are a minor.");
}

// if...else if...else statement
if (age < 13) {
  console.log("You are a child.");
} else if (age < 18) {
  console.log("You are a teenager.");
} else {
  console.log("You are an adult.");
}

// switch statement
let day = "Monday";
switch (day) {
  case "Monday":
    console.log("Start of the work week.");
    break;
  case "Friday":
    console.log("End of the work week!");
    break;
  case "Saturday":
  case "Sunday":
    console.log("Weekend!");
    break;
  default:
    console.log("It's a weekday.");
    break;
}

// Ternary operator
let message = age >= 18 ? "You can vote!" : "You're too young to vote.";

// Using truthy and falsy values
let username = "";
if (username) {
  console.log("Hello, " + username);
} else {
  console.log("Hello, guest!");
}
Try It Yourself!

Write a conditional statement that checks if a number is positive, negative, or zero!

JavaScript Editor
Mini Quiz

Test your knowledge with these quick questions!

1. When does the code in an if statement run?

Always
When the condition is true
When the condition is false
Only when there's no else statement

2. What's the purpose of the 'break' statement in a switch?

It's optional and doesn't do anything
It ends the entire JavaScript program
It stops execution of the switch and jumps to the code after it
It breaks the computer if used incorrectly
Your Progress
Lesson ProgressIn Progress
Est. time: 15-20 min
75 points
Module Progress
JavaScript Fundamentals2/10 completed

Complete all lessons to earn the Code Fundamentalist badge!

Rewards

Complete this lesson

Earn 75 points

Complete this module

Earn the Code Fundamentalist badge