Equality and Conditional operators
Equality operators
The == and === operators check whether two values are the same, but with two definitions of sameness.
- === strict equality operator, value and type are equivalent
- == relaxed sameness - allows type conversion
console.log("10" == 10);// true
console.log("10" === 10);// false
note
The === operator does not perform type coercion. It checks both the value and the type of the
operands. Since "10" is a string and 10 is a number, their types are different. Therefore, the
strict equality operator returns false.
Conditional Operator
The ?: operator is powerful in web development because it’s an expression,
not a statement—so you can use it inline ( in { } ) where if/else simply isn’t allowed.
condition ? expressionIfTrue : expressionIfFalse;
The first operand is evaluated as a boolean. If true, the second operand is evaluated and returned. Otherwise, the third operand is evaluated and returned.
// Instead of the if / else:
greeting = "hello ";
if (username) {
greeting += username;
} else {
greeting += "there";
}
// ternary expression
greeting = "hello " + (username ? username : "there");
``