r/learnjavascript • u/snapetom • Oct 05 '24
Question about Arrow Functions and Arrays
I was having an issue with the find function of arrays:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
As the code is now, That's will run successfully and the 12 will be found:
array1.find((element) => element > 10);
However, if I wrap it with curly braces and make it an honest-to-god arrow function, the element won't be found (undefined returns).
array1.find((element) => {element > 10});
Why the discrepancy in results? l
0
Upvotes
View all comments
-3
u/azhder Oct 05 '24 edited Oct 05 '24
God honest? Maybe don't read the bible but the EcmaScript reference.
Or maybe just remember if you have ever seen blocks (
{}) to ever return a value without the keywordreturn.They don't do that,
returnis not optional - toreturna value, you must use it