3 ms·
Do you mean true ? console.log("foo") : console.log("bar") ? This is already there: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Ope
by kamyarg 7y ago
Do you mean
true ? console.log("foo") : console.log("bar")
?
This is already there: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- james-mcelwain 7y agox ?: 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]
- robinson7d 7y agoIt’s more than simple ternary, for example: `response?.data?.users?[0]` To get the first user from a server response would be significantly longer if you wrote three nested ternary operators.