Comparison Operators
Comparisons are the heart of decision-making in JavaScript. You'll use them in conditions, loops, and filters. The key distinction is between equality (==) and identity (===).
Equality (==) vs Identity (===)
console.log(5 == "5");
console.log(5 === "5");
console.log(0 == false);
console.log(0 === false);
console.log(null == undefined);
console.log(null === undefined);
Try it Yourself โ
== coerces types before comparing. === requires both value and type to match.
Inequality (!= vs !==)
console.log(5 != "5");
console.log(5 !== "5");
Try it Yourself โ
Greater / Less Than
console.log(10 > 5);
console.log(5 >= 5);
console.log(3 < 1);
console.log(3 <= 3);
Try it Yourself โ
Logical AND (&&) and OR (||)
let age = 25;
let hasLicense = true;
console.log(age >= 18 && hasLicense);
console.log(age >= 18 || hasLicense);
let name = "";
console.log(name || "Guest");
console.log(name && "Has name");
Try it Yourself โ
|| returns the first truthy value. && returns the first falsy value.