DEV Community

Better loops in JavaScript

Kartik Malik on December 16, 2018

In this post, we will take a look at forms of for loop, array methods using which you can make your code simpler, more readable. These loops / me...
Collapse
 
joelnet profile image
JavaScript Joel

Great article! I noticed some minor bugs:

// This:
arr.reduce((accumulator, currentValue) => (acc += curr), 0)

// Should be:
arr.reduce((acc, curr) => (acc + curr), 0)

You can change += to just be + also.

Same goes for:

// This:
arr.reduce((acc, curr) => (acc *= curr), 1)

// Can be:
arr.reduce((acc, curr) => (acc * curr), 1)

Cheers!

Collapse
 
kartik2406 profile image
Kartik Malik

Good catch :D thank you very much :)

Collapse
 
nielsbom profile image
Niels Bom

Nice article!

If people are interested to read more on this style of programming in JS, it's called "functional programming". See this article for an introduction: dev.to/leandrotk_/functional-progr...

A really nice library for doing functional-style programming with JS: ramdajs

Collapse
 
hamedqaderi profile image
Hamm 🐘

Nice article. Thanks, Kartik. By the way, there is a little note to mention. It's better to use (for in) for objects and (for of) for arrays. Because (for in) has been optimized for Objects and not arrays.

Collapse
 
kartik2406 profile image
Kartik Malik

Oh cool, I didn't know that. Thanks for sharing :)

Collapse
 
summerkiflain profile image
Summer Kiflain

Nice article.

//sum of array elements
arr.reduce((accumulator, currentValue) => (acc += curr), 0)

should be:

//sum of array elements
arr.reduce((accumulator, currentValue) => (accumulator += currentValue), 0)

Collapse
 
kartik2406 profile image
Kartik Malik

Changed it. Thank you :)

Collapse
 
justintime4tea profile image
Justin Gross

Awesome write up! I'm glad you mentioned the less known ones as well like some and every. An additional note on the "for of" and forEach regarding accessing the index of an element would be nice. If you're interested in continuing to write about working with Arrays, the spread, rest, destructuring, Array.from and using asynchronous calls within loops would be another welcome write up!

Collapse
 
kartik2406 profile image
Kartik Malik

Yes these are interesting ideas will write about them soon :).

Collapse
 
juanmamenendez15 profile image
Juan Manuel Menendez Hernandez

// checking if at least one of them is 18 or above
arr.some(val => val >= 18)

should be:

// checking if at least one of them is 18 or above
friends.some(val => val >= 18)

Good article, but next time you need to check the examples in the console men, you can do it better ;)

Collapse
 
kartik2406 profile image
Kartik Malik

Hehe thanks. Did run the examples on console, changed the names later though hence the issue. Will be careful next time :)

Collapse
 
weirdmayo profile image
Daniel Mayovsky

I should have known about array.every() earlier than today. Would've saved me time and brains

Collapse
 
kartik2406 profile image
Kartik Malik

Thanks for the link it's a nice read to get into performant code :).