4 ms·
x ?: y is shorthand for x != null ? x : y
by james-mcelwain 7y ago
x ?: y
is shorthand for
x != null ? x : y
- kamyarg 7y agoAh, thanks for the clarification. Did not know it is supported in some languages.
- jchook 7y agoIn JavaScript you can simply use the logical or operator x || y
- rimunroe 7y agoThis is not equivalent. It will result in y if x is any falsy value (like false, empty string, or 0), not just if x is nullish.
- jchook 7y agoIn which languages does ?: not work that way? For example in PHP, it works exactly like you describe.
- nojvek 7y agoI think that’s exactly what the nullish ?? Operator is. You can write x ?? y Which is x !== null and x != undefined ? x : y
- james-mcelwain 7y agoKotlin.
- deleted 7y ago[deleted]