foreach loop syntax in javascript -
consider myarray array of objects.
are these 2 pieces of code equivalent ?
myarray.foreach((item) => { return doanaction(item); }); myarray.foreach((item) => { doanaction(item); });
is there of them better in terms of syntax? if yes, why ?
the better question is, why not use function directly callback, like
myarray.foreach(doanaction);
the callback must follow api of array#foreach
, is
function execute each element, taking 3 arguments:
- currentvalue: current element being processed in array.
- index: index of current element being processed in array.
- array: array that
foreach()
being applied to.
if need element, use given callback directly without mapping.
Comments
Post a Comment