News for hackers. Stuff that matters.
Re:New ES2019 JavaScript features by a13n on Thursday September 26, 2019 @07:11PM (#21086635)
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.

Re:New ES2019 JavaScript features by rmrfrmrf on Thursday September 26, 2019 @07:56PM (#21086984)
Optional chaining is moving through committee along with the "nullish coalescing operator" represented as `??`, which works like `||` except only falls through with `null` and `undefined`.
Re:New ES2019 JavaScript features by kamyarg on Thursday September 26, 2019 @08:03PM (#21087044)
Do you mean

    true ? console.log("foo") : console.log("bar")
?

This is already there: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:New ES2019 JavaScript features by robinson7d on Thursday September 26, 2019 @08:21PM (#21087166)
It’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.

Re:New ES2019 JavaScript features by james-mcelwain on Thursday September 26, 2019 @08:07PM (#21087073)

  x ?: y 
is shorthand for

  x != null ? x : y
Re:New ES2019 JavaScript features by jchook on Thursday September 26, 2019 @08:43PM (#21087338)
In JavaScript you can simply use the logical or operator

x || y

Re:New ES2019 JavaScript features by rimunroe on Thursday September 26, 2019 @08:54PM (#21087420)
This 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.
Re:New ES2019 JavaScript features by jchook on Friday September 27, 2019 @01:16AM (#21088670)
In which languages does ?: not work that way?

For example in PHP, it works exactly like you describe.

Re:New ES2019 JavaScript features by nojvek on Friday September 27, 2019 @01:43AM (#21088785)
I think that’s exactly what the nullish ?? Operator is.

You can write x ?? y

Which is x !== null and x != undefined ? x : y

Re:New ES2019 JavaScript features by james-mcelwain on Friday September 27, 2019 @11:42AM (#21093170)
Kotlin.
Re:New ES2019 JavaScript features by kamyarg on Thursday September 26, 2019 @08:15PM (#21087124)
Ah, thanks for the clarification. Did not know it is supported in some languages.
Re:New ES2019 JavaScript features by strbean on Thursday September 26, 2019 @07:48PM (#21086916)
How would this differ from the idiomatic "x || y"? Just s/non-zero/truthy
Re:New ES2019 JavaScript features by nojvek on Friday September 27, 2019 @01:44AM (#21088794)
|| short circuits on falsey. 0, “”, false, null, undefined

?? Short circuits on nullish. Only null and undefined.

Re:New ES2019 JavaScript features by saagarjha on Thursday September 26, 2019 @07:19PM (#21086705)
I think that's a GCC C extension, not an Objective-C feature.
Re:New ES2019 JavaScript features by a13n on Thursday September 26, 2019 @07:22PM (#21086737)
Sure, I still miss it from Objective-C :P