4 ms·
Super duper excited about optional chaining! One thing I miss from Objective-C that I wish JS would add is the `?:` operator – basically a shortened version of
by a13n 7y ago
Super duper excited about optional chaining!
One thing I miss from Objective-C that I wish JS would add is the `?:` operator – basically a shortened version of the ternary operator.
- saagarjha 7y agoI think that's a GCC C extension, not an Objective-C feature.
- a13n 7y agoSure, I still miss it from Objective-C :P
- strbean 7y agoHow would this differ from the idiomatic "x || y"? Just s/non-zero/truthy
- deleted 7y ago[deleted]
- nojvek 7y ago|| short circuits on falsey. 0, “”, false, null, undefined ?? Short circuits on nullish. Only null and undefined.
- rmrfrmrf 7y agoOptional chaining is moving through committee along with the "nullish coalescing operator" represented as `??`, which works like `||` except only falls through with `null` and `undefined`.
- kamyarg 7y agoDo 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 ago