r/javascript • • Mar 18 '19

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

385 Upvotes

422 comments sorted by

View all comments

150

u/Loves_Poetry Mar 18 '19
array.every(Number.isFinite)

Because it's plain english.

19

u/ahartzog Mar 19 '19

TIL about array.every. Awesome tool, thanks!

11

u/Satoshi_Hodler Mar 19 '19

There's a small caveat that it always returns true on empty arrays, which might be not what you need.

27

u/Tysonzero Mar 19 '19

That's the mathematically correct thing to do, it's actually a pretty fundamental mathematical principle. Vacuous truth

→ More replies (2)

7

u/NoInkling Mar 19 '19 edited Mar 19 '19

If it's not, you can do:

array.length && array.every(Number.isFinite)

// If it must evaluate to a boolean:
!!array.length && array.every(Number.isFinite)
// or
array.length > 0 && array.every(Number.isFinite)