I've see both x || [] and x ?? [] used for providing a fallback value if x is nullish. Are there any cases where these two give different results?
If x was a non-nullish falsey value, it would be different.
x = 0;
x = x ?? []
console.log(x);
y = null;
y = y ?? []
console.log(y);
These expressions x || [] and x ?? [] are logical assignments in Javascript.
x ?? [] is used to represent null or undefined cases while x || [] represent true if either a or b is true.
x ?? [] works by evaluating if the left side of the expression is null or undefined.
x || [] works by evaluating if a or b is true. If a is true continue in the if statement. If b is true, continue in the if statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With