I think “These three functions could probably remove thousands of lines of your most confusing buggy code.“ is a bit of an overstatement.
Also: there are situations where you’d rather have a for loop than map: if you want to mutate the array in-place (for performance reasons for example).
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
If you want to mutate an array in place, you could also use reduce. While it's recommend to create a new array, it is technically possible to mutate an existing array with reduce.
I think it depends. I mostly write React these days and I almost never use a for loop. The nice thing about maps is they implictly return an array, allowing you to avoid all imperative code. They also make it so you are not writing code with side effects.
Performance might be a reason for a for loop, but that eliminates the gains that you get from having immutable data structures in your code.
Though I agree that immutability and functional programming are very useful, and I like writing that style of code, you can always come across a situation where a certain amount of performance is necessary so that creating a lot of new data structures or very big ones (like map does) is not an option.
Array.map is very useful yes.
I think “These three functions could probably remove thousands of lines of your most confusing buggy code.“ is a bit of an overstatement.
Also: there are situations where you’d rather have a for loop than map: if you want to mutate the array in-place (for performance reasons for example).
If you want to mutate an array in place, you could also use reduce. While it's recommend to create a new array, it is technically possible to mutate an existing array with reduce.
I think it depends. I mostly write React these days and I almost never use a for loop. The nice thing about maps is they implictly return an array, allowing you to avoid all imperative code. They also make it so you are not writing code with side effects.
Performance might be a reason for a for loop, but that eliminates the gains that you get from having immutable data structures in your code.
Though I agree that immutability and functional programming are very useful, and I like writing that style of code, you can always come across a situation where a certain amount of performance is necessary so that creating a lot of new data structures or very big ones (like map does) is not an option.
it is an overstatement if you are a strong developer writing clear easy to follow loops. Newbies rarely do, good point about perf though!