Understanding conditional operators in javascript
Conditional (Ternary) Operator (?):
The ? symbol is part of the conditional (ternary) operator in JavaScript. It's used for creating conditional expressions.
The syntax is condition ? expr1 : expr2. If the condition evaluates to true, expr1 is executed; otherwise, expr2 is executed.
Example:
let age = 20;
let status = (age >= 18) ? 'adult' : 'minor';
console.log(status); // Output: 'adult'
Nullish Coalescing Operator (??):
The ?? symbol is the nullish coalescing operator introduced in ECMAScript 2020 (ES11).
- It's used to provide a default value for a variable if the variable is
nullorundefined. - It differs from the logical OR (
||) operator in that it only checks fornullorundefined, not other falsy values like0,false,'', etc. - The syntax is
expr1 ?? expr2. Ifexpr1isnullorundefined, it evaluates toexpr2. - Example:
let user = {
name: 'John',
age: 25,
address: null
};
let address = user.address ?? 'Unknown';
console.log(address); // Output: 'Unknown'
In summary, ? is used for conditional expressions, while ?? is used for providing default values for nullish variables.