|
| 1 | +/** |
| 2 | + * ======================================================== |
| 3 | + * Nullish Coalescing Operator (??) |
| 4 | + * ======================================================== |
| 5 | + * The nullish coalescing operator (??) is a logical operator that returns the right-hand operand |
| 6 | + * when the left-hand operand is null or undefined; otherwise, it returns the left-hand operand. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * ======================================================== |
| 11 | + * Basic Usage |
| 12 | + * ======================================================== |
| 13 | + * It is often used to assign default values to variables. |
| 14 | + */ |
| 15 | +const name = null; |
| 16 | +const defaultName = "John Doe"; |
| 17 | +const username = name ?? defaultName; |
| 18 | +console.log(`Username: ${username}`); // Output: 'Username: John Doe' |
| 19 | + |
| 20 | +/** |
| 21 | + * ======================================================== |
| 22 | + * Difference from Logical OR (||) |
| 23 | + * ======================================================== |
| 24 | + * The nullish coalescing operator only checks for null or undefined, |
| 25 | + * while the logical OR operator (||) checks for any falsy value. |
| 26 | + */ |
| 27 | +const zeroValue = 0; |
| 28 | +const orResult = zeroValue || "default"; |
| 29 | +const nullishResult = zeroValue ?? "default"; |
| 30 | +console.log(`OR Result: ${orResult}`); // Output: 'OR Result: default' |
| 31 | +console.log(`Nullish Result: ${nullishResult}`); // Output: 'Nullish Result: 0' |
| 32 | + |
| 33 | +/** |
| 34 | + * ======================================================== |
| 35 | + * Combining with Ternary Operator |
| 36 | + * ======================================================== |
| 37 | + * The nullish coalescing operator can be combined with the ternary operator for more complex conditions. |
| 38 | + */ |
| 39 | +const age = null; |
| 40 | +const ageValue = age ?? (age === 0 ? "Zero" : "Undefined"); |
| 41 | +console.log(`Age Value: ${ageValue}`); // Output: 'Age Value: Undefined' |
| 42 | + |
| 43 | +/** |
| 44 | + * ======================================================== |
| 45 | + * Use in Object Properties |
| 46 | + * ======================================================== |
| 47 | + * You can also use it to provide default values for object properties. |
| 48 | + */ |
| 49 | +const user = { |
| 50 | + firstName: "Jane", |
| 51 | +}; |
| 52 | +const lastName = user?.lastName ?? "Doe"; |
| 53 | +console.log(`User's Last Name: ${lastName}`); // Output: 'User's Last Name: Doe' |
| 54 | + |
| 55 | +/** |
| 56 | + * ======================================================== |
| 57 | + * Nuances and Advanced Techniques |
| 58 | + * ======================================================== |
| 59 | + */ |
| 60 | + |
| 61 | +/** |
| 62 | + * 1. Short-Circuiting |
| 63 | + * -------------------- |
| 64 | + * Similar to other logical operators, the nullish coalescing operator short-circuits, |
| 65 | + * meaning it won't evaluate the right-hand expression if it doesn't need to. |
| 66 | + */ |
| 67 | + |
| 68 | +/** |
| 69 | + * 2. Operator Precedence |
| 70 | + * ---------------------- |
| 71 | + * Be cautious with operator precedence when combining nullish coalescing with other operators. |
| 72 | + * Using parentheses can help make the behavior explicit. |
| 73 | + */ |
| 74 | +const result = 5 + (null ?? 2); // This will be 5 + 2 = 7 |
| 75 | + |
| 76 | +/** |
| 77 | + * 3. Type Considerations |
| 78 | + * ----------------------- |
| 79 | + * Nullish coalescing is useful for avoiding unexpected type conversions, |
| 80 | + * particularly when dealing with possible null or undefined values. |
| 81 | + */ |
| 82 | + |
| 83 | +/** |
| 84 | + * 4. Use Cases |
| 85 | + * ------------ |
| 86 | + * It's commonly used in function arguments, object destructuring, and variable assignments |
| 87 | + * to provide default values only when a variable is null or undefined, but not for other falsy values. |
| 88 | + */ |
0 commit comments