r/learnjavascript • • May 15 '19

7 Useful JavaScript Tricks

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

18 comments sorted by

View all comments

3

u/delventhalz May 15 '19

I dig most of these, but boolean conversion for filter is entirely redundant. Just use identity: .filter(x => x). And the empty object thing seems both pointless and potentially confusing. What is the use case for that?

8

u/jaredcheeda May 15 '19

.filter(Boolean) is much cleaner to read and faster to visually parse. directly conveys intent, and avoids arbitrary use of single character variable names.

2

u/delventhalz May 15 '19

Agree to disagree there. To me, “x => x” just reads as “identity”, and the intent is very clear. Using Boolean leads me to believe there is extra work going on for some purpose. There isn’t. So I find it misleading.

For what it’s worth, you could also use a more specific variable name depending on your use case.