For loops versus map/filter/reduce functions

I’m working through the Javascript Algorithms And Data Structures Certification. I’m stumbling on the advantage of map, filter and reduce functions over the use of for loops. I’ll spend 30 minutes on the functional programming challenges that disallow for loops and five minutes on the later challenges allowing for loops.

Will the use of for loops instead of built in functions cause a potential employer to pass a candidate over?

Do the professionals use these built in functions instead of commenting on what they’re trying to accomplish?

Do the built in functions have any significant advantage on a webpage?

I found an article touting the benefits at

I’ll be polite and agree to disagree on the claims until I’m more experienced.

With more practice, you may find it faster to use map, filter, or reduce in certain circumstances.

These functions can make code much for readable in certain situations, so I would think an employer is going to expect you to know when and when not to use such methods.

Comments should not explain what you are trying to accomplish with your code, but instead should explain why you may choose a certain way of accomplishing something. Typically, it is to clarify intent of a variable’s usage or when using non-typical syntax.

Depending on the browser, the functions may actual run slower than traditional for or while loops, but the main idea is they abstract the “how” away and focus on making the code more readable and easier to test.

If you have a situation where you are already given an array and need to modify each value in the array in some way and then return the array, then the map method is a perfect fit. There are times, when using a method like reduce, map, or filter only makes the code complex. With experience, you will learn the exceptions of when not to use higher order functions like reduce, map, or filter. When I first was introduced to the methods, I was a little hesitant, because it was different than the traditional for and while loops that I had always used. Now, almost 95% of the problems I encounter on a daily basis involving iterating over arrays is easily solved with one of the many higher order functions. Just give it some time and practice, practice, practice.

Agreeing with Randy, sometimes the prototype methods are easier and cleaner. When I see a map or reduce or sort method, I instantly have an idea of what it is trying to do and a quick look at the callback tells me how. If it is buried in a for loop, then I have to take a second to figure it out.

There certainly are cases where a for loop is better. But all things being equal, prototype methods are usually better if you have a choice.

So yes, I if I were interviewing someone and they needed to add up all the numbers in an array, and they didn’t use reduce, it would make me wonder how well they know JS. Would be be passed over? Maybe, maybe not. But you would probably loose points for it.

Sometimes one is better and sometimes the other is. The ability to tell the difference comes from experience. If you don’t know which is better, then you lack that experience.

I recently had a coding interview where I started to use a for loop and then almost immediately switched to a while loop. When the person was evaluating me, he commented that I should have instinctively known that while was a better choices. Either would have worked, but while was cleaner. Every little bit helps.