Module 3: Operators and User Interaction
Logical and Comparison Operators
Comparison Operators:
Compare two values and return a boolean (true
or false
).
==
(Equal to): Checks if values are equal (performs type coercion).5 == "5"
istrue
.===
(Strictly equal to): Checks if values AND types are equal.5 === "5"
isfalse
.!=
(Not equal to):5 != 8
istrue
.!==
(Strictly not equal to):5 !== "5"
istrue
.>
(Greater than):5 > 3
istrue
.<
(Less than):5 < 3
isfalse
.>=
(Greater than or equal to):5 >= 5
istrue
.<=
(Less than or equal to):5 <= 3
isfalse
.
Logical Operators: Combine or modify boolean expressions.
&&
(Logical AND): Returnstrue
if both operands aretrue
.true && false
isfalse
.||
(Logical OR): Returnstrue
if at least one operand istrue
.true || false
istrue
.!
(Logical NOT): Inverts the boolean value.!true
isfalse
.
let age = 20; let hasLicense = true; console.log(age >= 18 && hasLicense); // true console.log(age < 18 || !hasLicense); // false
Recommendation: Use strict equality (===
and !==
) to avoid unexpected behavior from type coercion.