Episode 7 — DSA with JavaScript / 7.1 — Conditional Statements

7.1 — Interview Questions: Conditional Statements

<< Overview


Beginner

Q1. What is the difference between == and === in JavaScript?

Answer: == (loose equality) performs type coercion before comparing, while === (strict equality) compares both value and type without coercion.

0 == ""       // true  — both coerced to 0
0 === ""      // false — number vs string
null == undefined   // true
null === undefined  // false
1 == "1"      // true
1 === "1"     // false

Best practice: Always use === and !== to avoid subtle bugs.

C++ note: C++ only has == which compares values of the same type. No coercion happens between unrelated types (compiler error instead).


Q2. What are falsy values in JavaScript? List all of them.

Answer: There are exactly 7 falsy values in JavaScript:

ValueType
falseboolean
0, -0number
0nBigInt
""string (empty)
nullnull
undefinedundefined
NaNnumber

Everything else is truthy, including [], {}, "0", and "false".

// Common gotcha
if ([]) console.log("truthy");    // prints! Empty array is truthy
if ("0") console.log("truthy");   // prints! Non-empty string is truthy

Q3. When should you use switch instead of if-else?

Answer: Use switch when:

  1. You are comparing one variable against multiple constant values
  2. There are many branches (5+ cases)
  3. You need fall-through behavior (grouping cases)

Use if-else when:

  1. You need range checks (e.g., x > 10)
  2. Conditions involve different variables
  3. Conditions have complex boolean expressions
// switch is cleaner here
switch (command) {
    case "start": start(); break;
    case "stop":  stop();  break;
    case "reset": reset(); break;
    default: unknown();
}

// if-else is better here (range check)
if (score >= 90) grade = "A";
else if (score >= 80) grade = "B";

Q4. What is short-circuit evaluation? Give an example.

Answer: Short-circuit evaluation means the second operand of && or || is only evaluated if needed.

  • &&: If the left side is falsy, the right side is never evaluated
  • ||: If the left side is truthy, the right side is never evaluated
// Safe property access (short-circuit prevents crash)
const name = user && user.name;

// Default value
const port = config.port || 3000;

// Conditional function call
isReady && initialize();

Why it matters: Prevents runtime errors (accessing properties of null) and enables concise patterns.


Q5. What is the difference between || and ?? in JavaScript?

Answer:

  • || returns the right side when the left is falsy (includes 0, "", false)
  • ?? returns the right side only when the left is null or undefined
const a = 0 || 10;    // 10 (0 is falsy)
const b = 0 ?? 10;    // 0  (0 is not null/undefined)

const c = "" || "default";   // "default"
const d = "" ?? "default";   // ""

const e = null ?? "fallback";  // "fallback"

Use ?? when 0, "", or false are valid values.


Intermediate

Q6. Explain the ternary operator. When should you avoid using it?

Answer: The ternary operator condition ? valueIfTrue : valueIfFalse is a concise alternative to if-else for expressions that produce a value.

Use it for:

  • Simple value assignment based on a condition
  • Inline values in template literals or JSX

Avoid when:

  • Logic is complex (multi-step operations)
  • Nesting would be needed (nested ternaries are hard to read)
  • Side effects are involved
// GOOD
const label = count === 1 ? "item" : "items";

// BAD — too complex, use if-else
const result = a > b ? (c > d ? (e > f ? 1 : 2) : 3) : 4;

Q7. How do you handle multiple conditions efficiently without deep nesting?

Answer: Three techniques:

1. Guard clauses (early return):

function process(data) {
    if (!data) return null;
    if (!data.isValid) return { error: "Invalid" };
    if (data.isProcessed) return data.result;
    return compute(data);
}

2. Object/Map lookup:

const handlers = {
    create: handleCreate,
    read: handleRead,
    update: handleUpdate,
    delete: handleDelete,
};
const handler = handlers[action];
if (handler) handler(payload);

3. Array of conditions:

const rules = [
    { test: (v) => v.length < 3, msg: "Too short" },
    { test: (v) => v.length > 50, msg: "Too long" },
    { test: (v) => !/[A-Z]/.test(v), msg: "Need uppercase" },
];
const errors = rules.filter(r => r.test(value)).map(r => r.msg);

Q8. What is a dangling else problem? How do you prevent it?

Answer: In C/C++, when you omit braces, an else binds to the nearest if, which may not be what you intended:

// Misleading indentation
if (a > 0)
    if (b > 0)
        cout << "both positive";
else
    cout << "a is not positive";  // Actually: "b is not positive"!

The else binds to if (b > 0), not if (a > 0).

Prevention: Always use braces {}:

if (a > 0) {
    if (b > 0) {
        cout << "both positive";
    }
} else {
    cout << "a is not positive";
}

Q9. Implement a function that classifies a triangle by its sides. Handle all edge cases.

Answer:

function classifyTriangle(a, b, c) {
    // Validate inputs
    if (a <= 0 || b <= 0 || c <= 0) return "Invalid: sides must be positive";
    // Triangle inequality theorem
    if (a + b <= c || a + c <= b || b + c <= a) return "Not a triangle";
    // Classify
    if (a === b && b === c) return "Equilateral";
    if (a === b || b === c || a === c) return "Isosceles";
    return "Scalene";
}
string classifyTriangle(double a, double b, double c) {
    if (a <= 0 || b <= 0 || c <= 0) return "Invalid: sides must be positive";
    if (a + b <= c || a + c <= b || b + c <= a) return "Not a triangle";
    if (a == b && b == c) return "Equilateral";
    if (a == b || b == c || a == c) return "Isosceles";
    return "Scalene";
}

Edge cases tested: zero/negative sides, degenerate triangle (a+b=c), floating-point equality issues.


Q10. What is the Luhn algorithm? Implement it using conditionals.

Answer: The Luhn algorithm validates credit card numbers. From the right, double every second digit; if the result > 9, subtract 9. Sum all digits. Valid if sum % 10 === 0.

function luhnCheck(cardNumber) {
    const digits = cardNumber.replace(/\s/g, "").split("").reverse();
    let sum = 0;
    for (let i = 0; i < digits.length; i++) {
        let d = parseInt(digits[i], 10);
        if (i % 2 === 1) {
            d *= 2;
            if (d > 9) d -= 9;
        }
        sum += d;
    }
    return sum % 10 === 0;
}

console.log(luhnCheck("4539 1488 0343 6467")); // true
#include <string>
using namespace std;

bool luhnCheck(const string& card) {
    int sum = 0, count = 0;
    for (int i = card.size() - 1; i >= 0; i--) {
        if (card[i] == ' ') continue;
        int d = card[i] - '0';
        if (count % 2 == 1) {
            d *= 2;
            if (d > 9) d -= 9;
        }
        sum += d;
        count++;
    }
    return sum % 10 == 0;
}

Advanced

Q11. How do you implement a rule engine using conditionals?

Answer: A rule engine evaluates a set of conditions and applies corresponding actions.

const rules = [
    {
        name: "High-value order",
        condition: (order) => order.total > 1000,
        action: (order) => { order.discount = 0.1; order.priority = "high"; },
    },
    {
        name: "Returning customer",
        condition: (order) => order.customer.orderCount > 5,
        action: (order) => { order.discount = (order.discount || 0) + 0.05; },
    },
    {
        name: "Bulk order",
        condition: (order) => order.items.length > 20,
        action: (order) => { order.shippingFree = true; },
    },
];

function applyRules(order) {
    for (const rule of rules) {
        if (rule.condition(order)) {
            rule.action(order);
        }
    }
    return order;
}

This replaces a massive if-else chain with a data-driven approach.


Q12. Explain how conditional logic is used in state machines. Design a traffic light.

Answer:

States: RED → GREEN → YELLOW → RED → ...

    ┌──────┐     ┌───────┐     ┌────────┐
    │ RED  │────▶│ GREEN │────▶│ YELLOW │──┐
    └──────┘     └───────┘     └────────┘  │
        ▲                                   │
        └───────────────────────────────────┘
function nextLight(current) {
    switch (current) {
        case "RED":    return "GREEN";
        case "GREEN":  return "YELLOW";
        case "YELLOW": return "RED";
        default: throw new Error(`Invalid state: ${current}`);
    }
}

// Simulate
let state = "RED";
for (let i = 0; i < 6; i++) {
    console.log(state);
    state = nextLight(state);
}
// RED → GREEN → YELLOW → RED → GREEN → YELLOW
#include <iostream>
#include <string>
using namespace std;

string nextLight(const string& current) {
    if (current == "RED")    return "GREEN";
    if (current == "GREEN")  return "YELLOW";
    if (current == "YELLOW") return "RED";
    throw runtime_error("Invalid state");
}

Q13. How do you handle conditional logic in a functional programming style?

Answer: Use pure functions that map inputs to outputs without mutation:

// Imperative (mutates)
function applyDiscount(order) {
    if (order.total > 100) {
        order.total *= 0.9;  // mutation!
    }
    return order;
}

// Functional (no mutation)
function applyDiscount(order) {
    const discount = order.total > 100 ? 0.9 : 1;
    return { ...order, total: order.total * discount };
}

Use pipe or compose for chains:

const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x);

const processOrder = pipe(
    validateOrder,
    applyDiscount,
    calculateTax,
    formatReceipt,
);

Q14. What are optional chaining and nullish coalescing? When do they replace conditionals?

Answer:

// Before (verbose conditionals)
let city;
if (user && user.address && user.address.city) {
    city = user.address.city;
} else {
    city = "Unknown";
}

// After (optional chaining + nullish coalescing)
const city = user?.address?.city ?? "Unknown";

?. short-circuits to undefined if any part of the chain is null/undefined. ?? provides a fallback only for null/undefined (not for 0, "", or false).

When to use: Property access chains on potentially null objects. When NOT to use: When you need to distinguish between "not found" and "empty string."


Q15. Compare error handling strategies: conditionals vs try-catch. When to use each?

Answer:

StrategyUse caseExample
ConditionalsExpected invalid statesif (!data) return null;
try-catchUnexpected runtime errorstry { JSON.parse(str) } catch {}
// Conditional: expected case
function safeDivide(a, b) {
    if (b === 0) return { error: "Division by zero" };
    return { value: a / b };
}

// try-catch: unexpected parsing failure
function parseConfig(json) {
    try {
        return JSON.parse(json);
    } catch (e) {
        console.error("Invalid config JSON:", e.message);
        return null;
    }
}

Rule: Use conditionals for control flow, try-catch for error recovery.


Quick-fire table

#QuestionOne-line answer
1Falsy values in JS?false, 0, -0, 0n, "", null, undefined, NaN
2== vs ===?== coerces types, === does not
3Default operator?`
4Ternary limit?Avoid nesting more than one level
5Switch fall-through?Execution continues without break
6Guard clause?Early return for invalid input
7Dangling else?else binds to nearest if; use braces
8&& short-circuit?Right side skipped if left is falsy
9Object lookup vs switch?Object lookup when returning values
10Luhn check?Double every 2nd digit from right, sum mod 10 = 0

Additional rapid-fire cards

RC-001

  • Q: What happens when you use = instead of == in an if condition?
  • A: Assignment returns the assigned value, which is truthy if non-zero — always use ===.

RC-002

  • Q: Can switch compare strings in JavaScript?
  • A: Yes, switch uses strict comparison (===) for each case.

RC-003

  • Q: What is the output of Boolean([])?
  • A: true — empty array is truthy in JavaScript.

RC-004

  • Q: How does optional chaining (?.) work?
  • A: Returns undefined instead of throwing if a property chain has null/undefined.

RC-005

  • Q: What does NaN === NaN return?
  • A: false — NaN is not equal to anything, including itself.

RC-006

  • Q: How do you check for null AND undefined at once?
  • A: Use value == null (loose) or value === null || value === undefined.

RC-007

  • Q: Can you use return inside a switch?
  • A: Yes, return exits the function and implicitly breaks out of the switch.

RC-008

  • Q: What is a guard clause?
  • A: An early return at the top of a function to handle invalid or edge cases.

RC-009

  • Q: How do you compare objects in JavaScript?
  • A: By reference — two different objects are never === even with same contents.

RC-010

  • Q: What is the output of typeof null?
  • A: 'object' — this is a known JavaScript bug from the first version.

RC-011

  • Q: Can ternary operators have side effects?
  • A: Yes, but it's a bad practice. Use if-else for side effects.

RC-012

  • Q: How does C++ handle switch on enums?
  • A: C++ switch works on integral types and enums, not strings.

RC-013

  • Q: What is the purpose of default in switch?
  • A: Handles any case not matched by other case labels.

RC-014

  • Q: How do you test multiple conditions with &&?
  • A: Both sides must be truthy; if the first is falsy, the second is not evaluated.

RC-015

  • Q: What is nullish coalescing operator (??)?
  • A: Returns right operand only when left is null or undefined (not 0, '', false).

RC-016

  • Q: How do you avoid deeply nested if statements?
  • A: Use guard clauses, extract functions, use lookup objects, or use early returns.

RC-017

  • Q: Can switch handle ranges in JavaScript?
  • A: No, switch only matches exact values. Use if-else for ranges.

RC-018

  • Q: What does !!value do?
  • A: Double negation converts any value to its boolean equivalent.

RC-019

  • Q: How do you implement early returns?
  • A: Check invalid conditions first and return immediately, keeping the happy path unindented.

RC-020

  • Q: What is the difference between if-else and ternary?
  • A: Ternary is an expression (returns value), if-else is a statement.

RC-021

  • Q: What happens when you use = instead of == in an if condition?
  • A: Assignment returns the assigned value, which is truthy if non-zero — always use ===.

RC-022

  • Q: Can switch compare strings in JavaScript?
  • A: Yes, switch uses strict comparison (===) for each case.

RC-023

  • Q: What is the output of Boolean([])?
  • A: true — empty array is truthy in JavaScript.

RC-024

  • Q: How does optional chaining (?.) work?
  • A: Returns undefined instead of throwing if a property chain has null/undefined.

RC-025

  • Q: What does NaN === NaN return?
  • A: false — NaN is not equal to anything, including itself.

RC-026

  • Q: How do you check for null AND undefined at once?
  • A: Use value == null (loose) or value === null || value === undefined.

RC-027

  • Q: Can you use return inside a switch?
  • A: Yes, return exits the function and implicitly breaks out of the switch.

RC-028

  • Q: What is a guard clause?
  • A: An early return at the top of a function to handle invalid or edge cases.

RC-029

  • Q: How do you compare objects in JavaScript?
  • A: By reference — two different objects are never === even with same contents.

RC-030

  • Q: What is the output of typeof null?
  • A: 'object' — this is a known JavaScript bug from the first version.

RC-031

  • Q: Can ternary operators have side effects?
  • A: Yes, but it's a bad practice. Use if-else for side effects.

RC-032

  • Q: How does C++ handle switch on enums?
  • A: C++ switch works on integral types and enums, not strings.

RC-033

  • Q: What is the purpose of default in switch?
  • A: Handles any case not matched by other case labels.

RC-034

  • Q: How do you test multiple conditions with &&?
  • A: Both sides must be truthy; if the first is falsy, the second is not evaluated.

RC-035

  • Q: What is nullish coalescing operator (??)?
  • A: Returns right operand only when left is null or undefined (not 0, '', false).

RC-036

  • Q: How do you avoid deeply nested if statements?
  • A: Use guard clauses, extract functions, use lookup objects, or use early returns.

RC-037

  • Q: Can switch handle ranges in JavaScript?
  • A: No, switch only matches exact values. Use if-else for ranges.

RC-038

  • Q: What does !!value do?
  • A: Double negation converts any value to its boolean equivalent.

RC-039

  • Q: How do you implement early returns?
  • A: Check invalid conditions first and return immediately, keeping the happy path unindented.

RC-040

  • Q: What is the difference between if-else and ternary?
  • A: Ternary is an expression (returns value), if-else is a statement.

RC-041

  • Q: What happens when you use = instead of == in an if condition?
  • A: Assignment returns the assigned value, which is truthy if non-zero — always use ===.

RC-042

  • Q: Can switch compare strings in JavaScript?
  • A: Yes, switch uses strict comparison (===) for each case.

RC-043

  • Q: What is the output of Boolean([])?
  • A: true — empty array is truthy in JavaScript.

RC-044

  • Q: How does optional chaining (?.) work?
  • A: Returns undefined instead of throwing if a property chain has null/undefined.

RC-045

  • Q: What does NaN === NaN return?
  • A: false — NaN is not equal to anything, including itself.

RC-046

  • Q: How do you check for null AND undefined at once?
  • A: Use value == null (loose) or value === null || value === undefined.

RC-047

  • Q: Can you use return inside a switch?
  • A: Yes, return exits the function and implicitly breaks out of the switch.

RC-048

  • Q: What is a guard clause?
  • A: An early return at the top of a function to handle invalid or edge cases.

RC-049

  • Q: How do you compare objects in JavaScript?
  • A: By reference — two different objects are never === even with same contents.

RC-050

  • Q: What is the output of typeof null?
  • A: 'object' — this is a known JavaScript bug from the first version.

RC-051

  • Q: Can ternary operators have side effects?
  • A: Yes, but it's a bad practice. Use if-else for side effects.

RC-052

  • Q: How does C++ handle switch on enums?
  • A: C++ switch works on integral types and enums, not strings.

RC-053

  • Q: What is the purpose of default in switch?
  • A: Handles any case not matched by other case labels.

RC-054

  • Q: How do you test multiple conditions with &&?
  • A: Both sides must be truthy; if the first is falsy, the second is not evaluated.

RC-055

  • Q: What is nullish coalescing operator (??)?
  • A: Returns right operand only when left is null or undefined (not 0, '', false).

RC-056

  • Q: How do you avoid deeply nested if statements?
  • A: Use guard clauses, extract functions, use lookup objects, or use early returns.

RC-057

  • Q: Can switch handle ranges in JavaScript?
  • A: No, switch only matches exact values. Use if-else for ranges.

RC-058

  • Q: What does !!value do?
  • A: Double negation converts any value to its boolean equivalent.

RC-059

  • Q: How do you implement early returns?
  • A: Check invalid conditions first and return immediately, keeping the happy path unindented.

RC-060

  • Q: What is the difference between if-else and ternary?
  • A: Ternary is an expression (returns value), if-else is a statement.

RC-061

  • Q: What happens when you use = instead of == in an if condition?
  • A: Assignment returns the assigned value, which is truthy if non-zero — always use ===.

RC-062

  • Q: Can switch compare strings in JavaScript?
  • A: Yes, switch uses strict comparison (===) for each case.

RC-063

  • Q: What is the output of Boolean([])?
  • A: true — empty array is truthy in JavaScript.

RC-064

  • Q: How does optional chaining (?.) work?
  • A: Returns undefined instead of throwing if a property chain has null/undefined.

RC-065

  • Q: What does NaN === NaN return?
  • A: false — NaN is not equal to anything, including itself.

RC-066

  • Q: How do you check for null AND undefined at once?
  • A: Use value == null (loose) or value === null || value === undefined.

RC-067

  • Q: Can you use return inside a switch?
  • A: Yes, return exits the function and implicitly breaks out of the switch.

RC-068

  • Q: What is a guard clause?
  • A: An early return at the top of a function to handle invalid or edge cases.

RC-069

  • Q: How do you compare objects in JavaScript?
  • A: By reference — two different objects are never === even with same contents.

RC-070

  • Q: What is the output of typeof null?
  • A: 'object' — this is a known JavaScript bug from the first version.

RC-071

  • Q: Can ternary operators have side effects?
  • A: Yes, but it's a bad practice. Use if-else for side effects.

RC-072

  • Q: How does C++ handle switch on enums?
  • A: C++ switch works on integral types and enums, not strings.

RC-073

  • Q: What is the purpose of default in switch?
  • A: Handles any case not matched by other case labels.

RC-074

  • Q: How do you test multiple conditions with &&?
  • A: Both sides must be truthy; if the first is falsy, the second is not evaluated.

RC-075

  • Q: What is nullish coalescing operator (??)?
  • A: Returns right operand only when left is null or undefined (not 0, '', false).

RC-076

  • Q: How do you avoid deeply nested if statements?
  • A: Use guard clauses, extract functions, use lookup objects, or use early returns.

RC-077

  • Q: Can switch handle ranges in JavaScript?
  • A: No, switch only matches exact values. Use if-else for ranges.

RC-078

  • Q: What does !!value do?
  • A: Double negation converts any value to its boolean equivalent.

RC-079

  • Q: How do you implement early returns?
  • A: Check invalid conditions first and return immediately, keeping the happy path unindented.

RC-080

  • Q: What is the difference between if-else and ternary?
  • A: Ternary is an expression (returns value), if-else is a statement.

RC-081

  • Q: What happens when you use = instead of == in an if condition?
  • A: Assignment returns the assigned value, which is truthy if non-zero — always use ===.

RC-082

  • Q: Can switch compare strings in JavaScript?
  • A: Yes, switch uses strict comparison (===) for each case.

RC-083

  • Q: What is the output of Boolean([])?
  • A: true — empty array is truthy in JavaScript.

RC-084

  • Q: How does optional chaining (?.) work?
  • A: Returns undefined instead of throwing if a property chain has null/undefined.

RC-085

  • Q: What does NaN === NaN return?
  • A: false — NaN is not equal to anything, including itself.

RC-086

  • Q: How do you check for null AND undefined at once?
  • A: Use value == null (loose) or value === null || value === undefined.

RC-087

  • Q: Can you use return inside a switch?
  • A: Yes, return exits the function and implicitly breaks out of the switch.

RC-088

  • Q: What is a guard clause?
  • A: An early return at the top of a function to handle invalid or edge cases.

RC-089

  • Q: How do you compare objects in JavaScript?
  • A: By reference — two different objects are never === even with same contents.

RC-090

  • Q: What is the output of typeof null?
  • A: 'object' — this is a known JavaScript bug from the first version.

RC-091

  • Q: Can ternary operators have side effects?
  • A: Yes, but it's a bad practice. Use if-else for side effects.

RC-092

  • Q: How does C++ handle switch on enums?
  • A: C++ switch works on integral types and enums, not strings.

RC-093

  • Q: What is the purpose of default in switch?
  • A: Handles any case not matched by other case labels.

RC-094

  • Q: How do you test multiple conditions with &&?
  • A: Both sides must be truthy; if the first is falsy, the second is not evaluated.

RC-095

  • Q: What is nullish coalescing operator (??)?
  • A: Returns right operand only when left is null or undefined (not 0, '', false).

RC-096

  • Q: How do you avoid deeply nested if statements?
  • A: Use guard clauses, extract functions, use lookup objects, or use early returns.

RC-097

  • Q: Can switch handle ranges in JavaScript?
  • A: No, switch only matches exact values. Use if-else for ranges.

RC-098

  • Q: What does !!value do?
  • A: Double negation converts any value to its boolean equivalent.

RC-099

  • Q: How do you implement early returns?
  • A: Check invalid conditions first and return immediately, keeping the happy path unindented.

RC-100

  • Q: What is the difference between if-else and ternary?
  • A: Ternary is an expression (returns value), if-else is a statement.

RC-101

  • Q: What happens when you use = instead of == in an if condition?
  • A: Assignment returns the assigned value, which is truthy if non-zero — always use ===.

RC-102

  • Q: Can switch compare strings in JavaScript?
  • A: Yes, switch uses strict comparison (===) for each case.

RC-103

  • Q: What is the output of Boolean([])?
  • A: true — empty array is truthy in JavaScript.

RC-104

  • Q: How does optional chaining (?.) work?
  • A: Returns undefined instead of throwing if a property chain has null/undefined.

RC-105

  • Q: What does NaN === NaN return?
  • A: false — NaN is not equal to anything, including itself.

RC-106

  • Q: How do you check for null AND undefined at once?
  • A: Use value == null (loose) or value === null || value === undefined.

RC-107

  • Q: Can you use return inside a switch?
  • A: Yes, return exits the function and implicitly breaks out of the switch.

RC-108

  • Q: What is a guard clause?
  • A: An early return at the top of a function to handle invalid or edge cases.

RC-109

  • Q: How do you compare objects in JavaScript?
  • A: By reference — two different objects are never === even with same contents.

RC-110

  • Q: What is the output of typeof null?
  • A: 'object' — this is a known JavaScript bug from the first version.

RC-111

  • Q: Can ternary operators have side effects?
  • A: Yes, but it's a bad practice. Use if-else for side effects.

RC-112

  • Q: How does C++ handle switch on enums?
  • A: C++ switch works on integral types and enums, not strings.

RC-113

  • Q: What is the purpose of default in switch?
  • A: Handles any case not matched by other case labels.

RC-114

  • Q: How do you test multiple conditions with &&?
  • A: Both sides must be truthy; if the first is falsy, the second is not evaluated.

RC-115

  • Q: What is nullish coalescing operator (??)?
  • A: Returns right operand only when left is null or undefined (not 0, '', false).

RC-116

  • Q: How do you avoid deeply nested if statements?
  • A: Use guard clauses, extract functions, use lookup objects, or use early returns.

RC-117

  • Q: Can switch handle ranges in JavaScript?
  • A: No, switch only matches exact values. Use if-else for ranges.

RC-118

  • Q: What does !!value do?
  • A: Double negation converts any value to its boolean equivalent.

RC-119

  • Q: How do you implement early returns?
  • A: Check invalid conditions first and return immediately, keeping the happy path unindented.

RC-120

  • Q: What is the difference between if-else and ternary?
  • A: Ternary is an expression (returns value), if-else is a statement.

RC-121

  • Q: What happens when you use = instead of == in an if condition?
  • A: Assignment returns the assigned value, which is truthy if non-zero — always use ===.

RC-122

  • Q: Can switch compare strings in JavaScript?
  • A: Yes, switch uses strict comparison (===) for each case.

RC-123

  • Q: What is the output of Boolean([])?
  • A: true — empty array is truthy in JavaScript.

RC-124

  • Q: How does optional chaining (?.) work?
  • A: Returns undefined instead of throwing if a property chain has null/undefined.

RC-125

  • Q: What does NaN === NaN return?
  • A: false — NaN is not equal to anything, including itself.

RC-126

  • Q: How do you check for null AND undefined at once?
  • A: Use value == null (loose) or value === null || value === undefined.

RC-127

  • Q: Can you use return inside a switch?
  • A: Yes, return exits the function and implicitly breaks out of the switch.

RC-128

  • Q: What is a guard clause?
  • A: An early return at the top of a function to handle invalid or edge cases.

RC-129

  • Q: How do you compare objects in JavaScript?
  • A: By reference — two different objects are never === even with same contents.

RC-130

  • Q: What is the output of typeof null?
  • A: 'object' — this is a known JavaScript bug from the first version.

RC-131

  • Q: Can ternary operators have side effects?
  • A: Yes, but it's a bad practice. Use if-else for side effects.

RC-132

  • Q: How does C++ handle switch on enums?
  • A: C++ switch works on integral types and enums, not strings.

RC-133

  • Q: What is the purpose of default in switch?
  • A: Handles any case not matched by other case labels.

RC-134

  • Q: How do you test multiple conditions with &&?
  • A: Both sides must be truthy; if the first is falsy, the second is not evaluated.

RC-135

  • Q: What is nullish coalescing operator (??)?
  • A: Returns right operand only when left is null or undefined (not 0, '', false).

RC-136

  • Q: How do you avoid deeply nested if statements?
  • A: Use guard clauses, extract functions, use lookup objects, or use early returns.

RC-137

  • Q: Can switch handle ranges in JavaScript?
  • A: No, switch only matches exact values. Use if-else for ranges.

RC-138

  • Q: What does !!value do?
  • A: Double negation converts any value to its boolean equivalent.

RC-139

  • Q: How do you implement early returns?
  • A: Check invalid conditions first and return immediately, keeping the happy path unindented.

RC-140

  • Q: What is the difference between if-else and ternary?
  • A: Ternary is an expression (returns value), if-else is a statement.

RC-141

  • Q: What happens when you use = instead of == in an if condition?
  • A: Assignment returns the assigned value, which is truthy if non-zero — always use ===.

RC-142

  • Q: Can switch compare strings in JavaScript?
  • A: Yes, switch uses strict comparison (===) for each case.

RC-143

  • Q: What is the output of Boolean([])?
  • A: true — empty array is truthy in JavaScript.

RC-144

  • Q: How does optional chaining (?.) work?
  • A: Returns undefined instead of throwing if a property chain has null/undefined.

RC-145

  • Q: What does NaN === NaN return?
  • A: false — NaN is not equal to anything, including itself.

RC-146

  • Q: How do you check for null AND undefined at once?
  • A: Use value == null (loose) or value === null || value === undefined.

RC-147

  • Q: Can you use return inside a switch?
  • A: Yes, return exits the function and implicitly breaks out of the switch.

RC-148

  • Q: What is a guard clause?
  • A: An early return at the top of a function to handle invalid or edge cases.

RC-149

  • Q: How do you compare objects in JavaScript?
  • A: By reference — two different objects are never === even with same contents.

RC-150

  • Q: What is the output of typeof null?
  • A: 'object' — this is a known JavaScript bug from the first version.

RC-151

  • Q: Can ternary operators have side effects?
  • A: Yes, but it's a bad practice. Use if-else for side effects.

RC-152

  • Q: How does C++ handle switch on enums?
  • A: C++ switch works on integral types and enums, not strings.

RC-153

  • Q: What is the purpose of default in switch?
  • A: Handles any case not matched by other case labels.

RC-154

  • Q: How do you test multiple conditions with &&?
  • A: Both sides must be truthy; if the first is falsy, the second is not evaluated.

RC-155

  • Q: What is nullish coalescing operator (??)?
  • A: Returns right operand only when left is null or undefined (not 0, '', false).

RC-156

  • Q: How do you avoid deeply nested if statements?
  • A: Use guard clauses, extract functions, use lookup objects, or use early returns.

RC-157

  • Q: Can switch handle ranges in JavaScript?
  • A: No, switch only matches exact values. Use if-else for ranges.

RC-158

  • Q: What does !!value do?
  • A: Double negation converts any value to its boolean equivalent.

RC-159

  • Q: How do you implement early returns?
  • A: Check invalid conditions first and return immediately, keeping the happy path unindented.

RC-160

  • Q: What is the difference between if-else and ternary?
  • A: Ternary is an expression (returns value), if-else is a statement.

RC-161

  • Q: What happens when you use = instead of == in an if condition?
  • A: Assignment returns the assigned value, which is truthy if non-zero — always use ===.

RC-162

  • Q: Can switch compare strings in JavaScript?
  • A: Yes, switch uses strict comparison (===) for each case.

RC-163

  • Q: What is the output of Boolean([])?
  • A: true — empty array is truthy in JavaScript.

RC-164

  • Q: How does optional chaining (?.) work?
  • A: Returns undefined instead of throwing if a property chain has null/undefined.

RC-165

  • Q: What does NaN === NaN return?
  • A: false — NaN is not equal to anything, including itself.

RC-166

  • Q: How do you check for null AND undefined at once?
  • A: Use value == null (loose) or value === null || value === undefined.

RC-167

  • Q: Can you use return inside a switch?
  • A: Yes, return exits the function and implicitly breaks out of the switch.

RC-168

  • Q: What is a guard clause?
  • A: An early return at the top of a function to handle invalid or edge cases.

RC-169

  • Q: How do you compare objects in JavaScript?
  • A: By reference — two different objects are never === even with same contents.

RC-170

  • Q: What is the output of typeof null?
  • A: 'object' — this is a known JavaScript bug from the first version.

RC-171

  • Q: Can ternary operators have side effects?
  • A: Yes, but it's a bad practice. Use if-else for side effects.

RC-172

  • Q: How does C++ handle switch on enums?
  • A: C++ switch works on integral types and enums, not strings.

RC-173

  • Q: What is the purpose of default in switch?
  • A: Handles any case not matched by other case labels.

RC-174

  • Q: How do you test multiple conditions with &&?
  • A: Both sides must be truthy; if the first is falsy, the second is not evaluated.

RC-175

  • Q: What is nullish coalescing operator (??)?
  • A: Returns right operand only when left is null or undefined (not 0, '', false).

RC-176

  • Q: How do you avoid deeply nested if statements?
  • A: Use guard clauses, extract functions, use lookup objects, or use early returns.

RC-177

  • Q: Can switch handle ranges in JavaScript?
  • A: No, switch only matches exact values. Use if-else for ranges.

RC-178

  • Q: What does !!value do?
  • A: Double negation converts any value to its boolean equivalent.

RC-179

  • Q: How do you implement early returns?
  • A: Check invalid conditions first and return immediately, keeping the happy path unindented.

RC-180

  • Q: What is the difference between if-else and ternary?
  • A: Ternary is an expression (returns value), if-else is a statement.

RC-181

  • Q: What happens when you use = instead of == in an if condition?
  • A: Assignment returns the assigned value, which is truthy if non-zero — always use ===.

RC-182

  • Q: Can switch compare strings in JavaScript?
  • A: Yes, switch uses strict comparison (===) for each case.

RC-183

  • Q: What is the output of Boolean([])?
  • A: true — empty array is truthy in JavaScript.

RC-184

  • Q: How does optional chaining (?.) work?
  • A: Returns undefined instead of throwing if a property chain has null/undefined.

RC-185

  • Q: What does NaN === NaN return?
  • A: false — NaN is not equal to anything, including itself.

RC-186

  • Q: How do you check for null AND undefined at once?
  • A: Use value == null (loose) or value === null || value === undefined.

RC-187

  • Q: Can you use return inside a switch?
  • A: Yes, return exits the function and implicitly breaks out of the switch.

RC-188

  • Q: What is a guard clause?
  • A: An early return at the top of a function to handle invalid or edge cases.

RC-189

  • Q: How do you compare objects in JavaScript?
  • A: By reference — two different objects are never === even with same contents.

RC-190

  • Q: What is the output of typeof null?
  • A: 'object' — this is a known JavaScript bug from the first version.

RC-191

  • Q: Can ternary operators have side effects?
  • A: Yes, but it's a bad practice. Use if-else for side effects.

RC-192

  • Q: How does C++ handle switch on enums?
  • A: C++ switch works on integral types and enums, not strings.

RC-193

  • Q: What is the purpose of default in switch?
  • A: Handles any case not matched by other case labels.

RC-194

  • Q: How do you test multiple conditions with &&?
  • A: Both sides must be truthy; if the first is falsy, the second is not evaluated.

RC-195

  • Q: What is nullish coalescing operator (??)?
  • A: Returns right operand only when left is null or undefined (not 0, '', false).

RC-196

  • Q: How do you avoid deeply nested if statements?
  • A: Use guard clauses, extract functions, use lookup objects, or use early returns.

RC-197

  • Q: Can switch handle ranges in JavaScript?
  • A: No, switch only matches exact values. Use if-else for ranges.

RC-198

  • Q: What does !!value do?
  • A: Double negation converts any value to its boolean equivalent.

RC-199

  • Q: How do you implement early returns?
  • A: Check invalid conditions first and return immediately, keeping the happy path unindented.

RC-200

  • Q: What is the difference between if-else and ternary?
  • A: Ternary is an expression (returns value), if-else is a statement.