r/webdev • • Apr 29 '19

7 Useful JavaScript Tricks

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

6 comments sorted by

View all comments

1

u/TaskForce_Kerim Apr 29 '19

The filter falsy values one is pretty nifty.

2

u/brown59fifty Apr 29 '19

Talking arrays, in the same way you can convert numbers to strings and strings to numbers with other type constructors.

I found that this kind of using functions is not widely known as it should be, so to make it more clear:

['0', '0b101', '0o101', '0x101'].map(Number)

is exactly same thing as

['0', '0b101', '0o101', '0x101'].map(item => Number(item))

(which results with [0, 5, 65, 257] ;)

1

u/TaskForce_Kerim Apr 30 '19

I found that this kind of using functions is not widely known as it should be

Depends on what you mean. Using function references like map(Number) is very common. It's essentially the same as .on("click", someHandlerFunction). What's clever about .map(Boolean) or .map(Number) is referencing the type constructor.