JavaScript Operators and Conditions

== vs ===

JavaScript
console.log(5 == "5");    // loose: converts types
console.log(5 === "5");   // strict: no conversion
console.log(null == undefined, null === undefined);
console.log(0 == false, "" == 0);
true false true false true true
Always use ===Strict equality avoids surprising type conversions.

Logical Operators and Nullish Coalescing

JavaScript
const user = { name: "Ravi", age: 0 };
console.log(user.age || 18);          // || treats 0 as falsy
console.log(user.age ?? 18);          // ?? only replaces null/undefined
console.log(user.address?.city);      // optional chaining
18 0 undefined

Falsy values: false, 0, -0, 0n, "", null, undefined, NaN. Everything else is truthy.

if-else and switch

JavaScript
const hour = 14;
if (hour < 12) console.log("Good morning");
else if (hour < 17) console.log("Good afternoon");
else console.log("Good evening");

const day = "SAT";
switch (day) {
    case "SAT":
    case "SUN": console.log("Weekend"); break;
    default:    console.log("Weekday");
}
Good afternoon Weekend
Track your progress through the JavaScript course