News for hackers. Stuff that matters.
Re:Modern Javascript: Everything you missed over the last 10... by globular-toast on Saturday May 15, 2021 @12:31PM (#27166634)
A for loop is an imperative construct and can be used to do literally anything. One has to read on into the body of the loop to understand what's going on. Worse, there could be a bug and now you have no idea what the programmer intended. Array.map, on the other hand, only does one thing: turns an array into another array. The intention is clear. It makes a program much easier to understand.
Re:Modern Javascript: Everything you missed over the last 10... by jjnoakes on Saturday May 15, 2021 @12:34PM (#27166671)
In javascript, what prevents the body of the map function from doing arbitrary stuff? Don't you have to read that to know for sure, just like a for loop?

In some languages the map function may be restricted to not have side effects, in which case I'd agree, but not javascript, correct?

Re:Modern Javascript: Everything you missed over the last 10... by django77 on Saturday May 15, 2021 @12:47PM (#27166787)
This is correct, but there are some guarantees such as the result will be an array of the same length, mapping function will be called for each element, function itself can be more easily tested in isolation and there are no imperative constructs (creating accumulator array outside of the scope, tracking the index, pushing to the accumulator array)

This lends itself very nicely to functional programming (especially with typescript IMHO)

Re:Modern Javascript: Everything you missed over the last 10... by globular-toast on Saturday May 15, 2021 @12:42PM (#27166739)
You can't stop bad programmers doing bad stuff. It's not about that. It's about communicating intention. Code as documentation.
Re:Modern Javascript: Everything you missed over the last 10... by lanstin on Saturday May 15, 2021 @02:37PM (#27167736)
There is nothing more clear than code as a way to communicate, at least for me. My writing in English obviously could be better but easier just to show what I mean.