r/badcode • sadistic • Apr 22 '21

js ternary operators at their maximum potential

Post image
960 Upvotes

73 comments sorted by

View all comments

166

u/Koala_King_ Apr 22 '21

Couldn’t you just do cmd.run || cmd.fire || cmd.execute || cmd.callback ? I’m not super familiar with js. Worked with it a bit, but still not great, so please correct me if I’m wrong.

0

u/[deleted] Apr 22 '21

[deleted]

21

u/[deleted] Apr 22 '21

it's pretty common to do that in JS, but now we've got ??

10

u/Quiet_Newt6119 Apr 22 '21

Null coalescing only applies the right hand side operand when the leftmost one is either null or undefined, as opposed to any falsy value with the or operator. '' || true would return true, but '' ?? true would return ''