Did you know?
What is the difference between ?? and ||?
From its sweet French name “Operator of coalescence of dummies”, a ?? b allows you to return the term a if the latter is neither null nor undefined. In the opposite case the operator returns the term b.
Here is an example that allows you to redefine this operator in JavaScript.
const result = a ?? b
const nullishCoalescingOperator = (a, b) => { if (a !== null && a !== undefined) { return a } return b; } const result = nullishCoalescingOperator(a,b);
The logical OR operator is similar to the null coalescence operator except that the latter tests whether the term a is falsy .
As a reminder, here is a non-exhaustive list of falsy values in JavaScript:
Here is an example that allows you to redefine this operator in JavaScript.
const result = a || b
const orOperator = (a,b) => { if (a) { return a; } return b; }
Finally, here is a table which summarizes the return of the functions ?? and ||
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR
The above is the detailed content of Did you know that? - ?? vs ||. For more information, please follow other related articles on the PHP Chinese website!