Out of curiosity, are the two options below functionally equivalent?
$array_variable = function_that_creates_an_array();
foreach($array_variable as $a){
do_something()
}
vs.
foreach(function_that_creates_an_array() as $a){
do_something()
}
Just want to make sure I'm not calling the function on every iteration or anything dumb like that.
Thanks!
You can't make JavaScript's forEach() function return a custom value. Using return in a forEach() is equivalent to a continue in a conventional loop.
'return' doesn't stop looping The reason is that we are passing a callback function in our forEach function, which behaves just like a normal function and is applied to each element no matter if we return from one i.e. when element is 2 in our case.
Yes, they are basically equivalent.
The only difference is the first one will add a variable to the current scope (i.e. if your in the global scope).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With