CHAPTER 5 · DECISIONS

JavaScript Conditions

Conditions program ko decision lene deti hain. JavaScript kisi expression ko evaluate karke decide kar sakta hai ki kaunsa code run hoga aur kaunsa skip hoga.

English + Hinglish32 min readPractice included

What is a condition?

A condition is an expression used to make a decision. JavaScript evaluates the expression in a boolean context and then chooses which block of code to execute.

decision.jsJS
const score = 75;

if (score >= 40) {
  console.log("Pass");
}
Hinglish Explanation

Condition ko question samjho: “Kya score 40 ya usse zyada hai?” Agar answer true hai to block run hota hai. Agar false hai to block skip ho jata hai.

The if statement

if runs a block only when its condition is truthy.

if.jsJS
const isLoggedIn = true;

if (isLoggedIn) {
  console.log("Welcome back");
}

Parentheses contain the condition and curly braces contain the code block.

if...else

Use else when you need an alternative path if the if condition is false.

if-else.jsJS
const age = 16;

if (age >= 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}

else if chains

Use else if when multiple mutually exclusive conditions need to be checked in order.

grades.jsJS
const score = 82;

if (score >= 90) {
  console.log("Grade A+");
} else if (score >= 75) {
  console.log("Grade A");
} else if (score >= 60) {
  console.log("Grade B");
} else if (score >= 40) {
  console.log("Pass");
} else {
  console.log("Try again");
}
Order matters: First matching branch runs and the rest of the chain is skipped. More specific or higher-threshold checks often need to appear first.

Conditions with comparison operators

Comparison operators from Chapter 4 are commonly used inside conditions.

comparisons.jsJS
const temperature = 31;

if (temperature > 30) {
  console.log("Hot day");
}

if (temperature === 31) {
  console.log("Exactly 31 degrees");
}

Prefer strict equality === when comparing values unless coercion is explicitly intended.

Combining conditions

Logical operators let you combine multiple checks.

logical-conditions.jsJS
const isLoggedIn = true;
const hasSubscription = true;

if (isLoggedIn && hasSubscription) {
  console.log("Open course");
}

const role = "editor";

if (role === "admin" || role === "editor") {
  console.log("Editing allowed");
}
  • && requires both sides to be truthy.
  • || needs at least one truthy side.
  • ! reverses truthiness.

Checking numeric ranges

JavaScript does not support mathematical chained comparisons such as 18 <= age <= 60. Write each comparison explicitly.

range.jsJS
const age = 25;

if (age >= 18 && age <= 60) {
  console.log("Within range");
}
Common mistake: 18 <= age <= 60 does not mean what it means in mathematics. Use && between separate comparisons.

Truthy and falsy in conditions

An if condition does not require a literal boolean. JavaScript converts the tested value to boolean truthiness.

truthy.jsJS
const userName = "Broun";

if (userName) {
  console.log("Name is available");
}

const message = "";

if (!message) {
  console.log("Message is empty");
}

Common falsy values include false, 0, empty string, null, undefined and NaN.

Explicit checks can be clearer

Truthy checks are concise, but explicit comparisons can communicate intent better when values such as 0 or an empty string are valid data.

explicit.jsJS
const cartCount = 0;

if (cartCount === 0) {
  console.log("Cart is empty");
}

Nested conditions

An if block can contain another condition, but too much nesting makes code harder to read.

nested.jsJS
const isLoggedIn = true;
const role = "student";

if (isLoggedIn) {
  if (role === "student") {
    console.log("Open student dashboard");
  }
}
Readability tip

Nested if valid hai, lekin 4–5 levels deep nesting ko avoid karo. Conditions ko simple aur readable rakhna debugging ko easy banata hai.

The switch statement

switch is useful when one value is compared against several exact cases.

switch.jsJS
const day = "monday";

switch (day) {
  case "monday":
    console.log("Start the week");
    break;
  case "friday":
    console.log("Almost weekend");
    break;
  default:
    console.log("Regular day");
}

switch matching uses strict comparison behavior. The default branch runs when no case matches.

Why break matters in switch

Without break, execution can continue into following cases. This is called fall-through.

fall-through.jsJS
const level = 1;

switch (level) {
  case 1:
    console.log("Beginner");
    break;
  case 2:
    console.log("Intermediate");
    break;
  default:
    console.log("Unknown level");
}
Intentional fall-through exists, but beginners should usually include break unless they deliberately want multiple cases to share behavior.

Grouping switch cases

Multiple cases can intentionally share the same block.

grouped-cases.jsJS
const role = "editor";

switch (role) {
  case "admin":
  case "editor":
    console.log("Can edit content");
    break;
  default:
    console.log("Read only");
}

Ternary operator

The conditional or ternary operator is a compact expression for choosing between two values.

ternary.jsJS
const score = 75;
const result = score >= 40 ? "Pass" : "Fail";

console.log(result);

Syntax: condition ? valueIfTrue : valueIfFalse.

Use ternary for simple choices. Complex nested ternaries are usually harder to read than normal if...else.

if...else vs switch vs ternary

  • Use if...else for ranges, multiple expressions and flexible conditions.
  • Use switch when one value is matched against several exact cases.
  • Use ternary for a short two-way value choice.

Practical access check

access.jsJS
const isLoggedIn = true;
const progress = 80;

if (!isLoggedIn) {
  console.log("Please sign in");
} else if (progress === 100) {
  console.log("Course complete");
} else if (progress >= 75) {
  console.log("Almost complete");
} else {
  console.log("Keep learning");
}

Beginner best practices

  • Conditions ko short and readable rakho.
  • Equality ke liye generally === and !== prefer karo.
  • Complex boolean logic me parentheses use karo when it improves clarity.
  • Range checks ko separate comparisons with && me likho.
  • switch me accidental fall-through avoid karo.
  • Nested ternaries avoid karo.
  • Real values such as 0 ko falsy shortcut se accidentally reject mat karo.

Common beginner mistakes

  • = ko condition me equality operator samajhna.
  • == aur === difference ignore karna.
  • else if conditions wrong order me rakhna.
  • 18 <= age <= 60 jaisi mathematical chaining likhna.
  • switch cases me break bhoolna.
  • Boolean logic ko unnecessarily deeply nest karna.
  • Complex nested ternary expressions banana.
  • Truthy/falsy behavior samjhe bina shortcuts use karna.

Chapter checklist

  • if, else and else if use kar sakte ho?
  • Logical operators ke saath conditions combine kar sakte ho?
  • Truthy/falsy values condition me kaise behave karti hain clear hai?
  • Numeric range correctly check kar sakte ho?
  • switch, case, break and default samajh aaye?
  • Ternary operator kab appropriate hai clear hai?

Practice Task — Decision Lab

Browser Console me conditions ki practice karo.

  1. age variable banao aur 18+ ke liye adult/minor message print karo.
  2. score se pass/fail condition banao.
  3. Score ranges ke liye 4-level else if grade system banao.
  4. isLoggedIn aur hasSubscription ko && ke saath combine karo.
  5. Role admin ya editor ho to access allow karo using ||.
  6. Age 18 se 60 ke range me hai ya nahi correctly check karo.
  7. Empty string aur non-empty string ko if me test karo.
  8. Weekday name ke liye switch statement banao with default.
  9. Ternary se score >= 40 ka result "Pass" ya "Fail" assign karo.
  10. Intentionally ek switch case se break remove karke fall-through observe karo; phir fix karo.