โœ…

JavaScript Booleans

Master logic, comparisons, and conditional decisions

โญ Beginner Friendly

What are Booleans?

Booleans represent one of two values: true or false. They're used for making decisions in your code - like checking if ingredients are fresh, if the oven is hot enough, or if a dish is ready to serve!

๐Ÿ’ก Real-World Example:

Is the milk expired? Is the water boiling? Has the timer finished? All these questions have yes/no (true/false) answers!

Boolean Operations

1. Comparison Operators

const temp = 180;
console.log(temp > 150);  // true
console.log(temp < 200);  // true
console.log(temp === 180); // true
console.log(temp !== 170); // true

2. Logical Operators (AND, OR, NOT)

const hasFlour = true;
const hasSugar = true;
const hasEggs = false;

console.log(hasFlour && hasSugar);  // true (both true)
console.log(hasFlour && hasEggs);   // false (one is false)
console.log(hasFlour || hasEggs);   // true (at least one true)
console.log(!hasEggs);              // true (opposite of false)

3. Truthy and Falsy Values

// Falsy values: false, 0, "", null, undefined, NaN
// Everything else is truthy!

console.log(Boolean(0));        // false
console.log(Boolean(""));       // false
console.log(Boolean("recipe")); // true
console.log(Boolean(42));       // true

4. Conditional Logic

const age = 25;
const hasLicense = true;

if (age >= 18 && hasLicense) {
    console.log("Can drive!");
} else {
    console.log("Cannot drive");
}

If/Else

if (temperature > 200) {
  console.log("Oven is hot!");
} else {
  console.log("Keep heating");
}

Else If

if (temp > 400) {
  console.log("Too hot!");
} else if (temp > 300) {
  console.log("Perfect!");
} else {
  console.log("Too cold");
}

Ternary Operator

let status = isReady 
  ? "Ready to serve" 
  : "Still cooking";

Switch Statement

switch(meal) {
  case "breakfast":
    console.log("๐Ÿณ");
    break;
  case "lunch":
    console.log("๐Ÿฅ—");
    break;
  default:
    console.log("๐Ÿฝ๏ธ");
}

Learn More

๐Ÿš€ Try It Yourself

Output:

๐Ÿ”

Game 1: Kitchen Inspector

Inspect food items and determine if they're safe to use! Practice boolean logic to check expiration dates and freshness.

๐Ÿฅซ Pantry Items

๐Ÿ“… Inspection Panel

Statistics:
๐Ÿ”ฅ

Game 2: Oven Ready Logic Game

Check if the oven meets ALL conditions before baking! Practice AND (&&) and OR (||) logic operators.

๐ŸŽ›๏ธ Oven Controls

200ยฐF
Preheated:
Door Closed:
Timer Set:

โœ… Ready Status

Logic Explanation:
๐Ÿ”

Challenge 1: Kitchen Inspector

๐Ÿ“‹ Your Mission:

Check if food items are safe to use by comparing their expiration date with today:

  1. Compare the expiration date (as days from today)
  2. Return true if the food is still good (expirationDays > 0)
  3. Return false if expired (expirationDays <= 0)

Example:

isFoodSafe(5) โ†’ true (expires in 5 days - safe!)
isFoodSafe(-2) โ†’ false (expired 2 days ago - unsafe!)

๐Ÿ’ก Hints:

  • Use the > comparison operator
  • Check if expirationDays is greater than 0

Test Results:

๐Ÿ”ฅ

Challenge 2: Oven Ready Logic

๐Ÿ“‹ Your Mission:

Determine if the oven is ready to bake. The oven is ready when:

  1. Temperature is at least 350ยฐF
  2. Preheating is complete
  3. The door is closed (NOT open)

Example:

isOvenReady(375, true, false) โ†’ true
isOvenReady(300, true, false) โ†’ false (too cold)

๐Ÿ’ก Hints:

  • Use && to combine multiple conditions
  • Use ! to negate (opposite) the doorOpen value
  • All three conditions must be true

Test Results: