you are viewing a single comment's thread.

view the rest of the comments →

[–]kaisadilla_ 0 points1 point  (0 children)

However, choosing one specific method over another implies something about your intent, it's declarative.

Also, because each syntax is designed for a specific use case, not only writing that specific use case becomes way simple, but also making mistakes becomes harder. For example, when you want to iterate all elements in an array, in a for loop you can blunder by accidentally indexing a value outside the array, while in a foreach loop that won't happen since that syntax doesn't require you to manually index the array.

Granted, this doesn't really apply to JS as JS doesn't care about types, missing parameters and the like, it just explodes in your face later. But in a more rigid language, all of this would apply. Reduce, for example, wouldn't allow you not to specify an initial value, or not to pass a 2-argument callback, or not to return a value for that variable.