I think it’s just everyone’s preference whether to use array.forEach or for-loop. My programming background is something that doesn’t have loops and I even tend to avoid array.forEach. This is an extreme case but I understand some people prefer arrays to loops.
I think it’s just everyone’s preference whether to use array.forEach or for-loop.
It's not just preference though - instantiating an array costs time and memory that's completely unnecessary, and is code that's non-idiomatic and confusing.
Arrays are for holding and iterating over values. for is for iterating over numbers.
Abusing arrays and constructing/garbage-collecting objects in memory just to do what a simple for loop could do more cheaply and readably is objectively bad code.
It sounds like you're just saying "I have no real reason for taking this sub-optimal path other than I don't understand the trade-offs involved and don't like for loops for no good reason".
If that's the case then I would strongly advise you to discard any phobias you have about unfamiliar constructs and concentrate on learning to write idiomatic code in a new language, or you're basically missing half the benefit of learning one.
1
u/dai-shi Mar 20 '19
I think it’s just everyone’s preference whether to use array.forEach or for-loop. My programming background is something that doesn’t have loops and I even tend to avoid array.forEach. This is an extreme case but I understand some people prefer arrays to loops.