r/webdev • • Apr 29 '19

7 Useful JavaScript Tricks

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

6 comments sorted by

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.

0

u/[deleted] Apr 29 '19

[deleted]

12

u/Mdude2312 full-stack Apr 29 '19

You might want to leave this subreddit if you believe you shouldn't use JavaScript...

0

u/[deleted] Apr 29 '19

[deleted]

5

u/heckdwreck Apr 29 '19

You might want to work on your understanding of the lack of intent and emotion able to be conveyed in text.

1

u/[deleted] Apr 29 '19

Curious to know what you'd use instead.