DEV Community

Discussion on: 9 Great Tips To Use Javascript Array Methods Like a Pro!

Collapse
 
dglsparsons profile image
Douglas Parsons

Great post. Some really good examples and clear improvements made.

One to be careful with though, is your point on Don't pass arguments from one function to another. It's more verbose, but sometimes you can get nasty surprises with functions taking more arguments than you might think!

For example:

['1', '7', '11'].map(parseInt) 
> [1, NaN, 3]
Enter fullscreen mode Exit fullscreen mode

Where the more explicit passing stops this weird behaviour.

['1', '7', '11'].map(n => parseInt(n))
> [1, 7, 11]
Enter fullscreen mode Exit fullscreen mode

This happens because map passes the index of the array you're operating on to parseInt as well as the number, and this totally changes the behaviour.

Collapse
 
galelmalah profile image
Gal Elmalah

Thanks, I’m glad you liked it :)
That’s a great point, thanks for pointing it out!