r/badcode • sadistic • Apr 22 '21

js ternary operators at their maximum potential

Post image
960 Upvotes

73 comments sorted by

View all comments

2

u/Sp1um Jun 09 '21

Not a JS dev, not sure if the reduce signature is correct

['run', 'fire', 'execute', 'callback'].reduce(null, (acc, k) => acc || cmd[k])

2

u/SpeckyYT sadistic Jun 09 '21

I don't think anyone would ever come on this idea, but that works quite well.

.reduce takes as first parameter the function, so you shouldn't add the null.

2

u/Sp1um Jun 09 '21

The null is supposed to be the accumulator initial value

2

u/SpeckyYT sadistic Jun 10 '21

The accumulator's initial value can be optionally set as the second parameter.