r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

Show parent comments

17

u/Peechez Mar 18 '19

I'm pretty sure if I tried to commit that I'd get slapped

1

u/ChronSyn Mar 18 '19

Why? It's an elegant solution to handling errors independently from your data - you could send your error to state, or logging, or any other source, but only if it fails, whereas if it succeeds, you've got data and can assign it to state and simply check for existence of the value before you start to work with it.

You could definitely split it out to multiple lines for readability without breaking the chain. It's no different from doing the different parts independently either.

4

u/Peechez Mar 18 '19

I mostly went in the one line format

5

u/catwhatcat Mar 19 '19

This post was about one liners and it's a nice one liner, but I would never commit that either for the sake of posterity + readability.

1

u/ChronSyn Mar 19 '19

I agree it's not really readable and why splitting it out over a few lines is better. Just for the sake that the thread was about one-liners is the reason it was all on one line.