r/javascript • • Apr 28 '19

7 Useful JavaScript Tricks

https://davidwalsh.name/javascript-tricks
505 Upvotes

36 comments sorted by

View all comments

8

u/DGCA Apr 29 '19

This is a pretty worthless comment, but if you were code golfing...

['', 123, 0, 'foo'].filter(Boolean)
['', 123, 0, 'foo'].filter(x=>!!x)

...the latter is one less character.

6

u/s1dd Apr 29 '19

This works too:

['', 123, 0, 'foo'].filter(x=>x)