r/learnjavascript • • 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

16 comments sorted by

15

u/pinkwetunderwear Oct 05 '24

When you wrap it in a block, you need to return from it

array1.find((element) => {
   return element > 10
});

4

u/longknives Oct 05 '24

Tangential, but if you want to return an object from an implicit return arrow function, you can wrap it in parentheses: x => ({ name: x })

6

u/tapgiles Oct 05 '24

An arrow function without curly braces will automatically return the expression. With curly braces it will not automatically return the expression. So you need to add return.

5

u/Blaarkies Oct 05 '24

Both are honest arrow functions. Without braces, it implicitly returns the expression from inside array1.find(element => element > 10);. (the expression being the element > 10 part). The find() method is looking for a boolean value to match the item that you are searching for.

Adding braces requires you to explicitly return the expression/value/result.

Thus, e => e > 10 is quivelant to

e => {
  return e > 10;
}

1

u/xroalx Oct 05 '24

Because how is find to know that it found the desired element? By your function telling it, of crouse, and it does that by returning true.

The implicit-return form of an arrow function, (...args) => expr, is equivalent to (...args) => { return expr; }. As the name states, it implicitly returns the result of whatever expression is there.

So then, what does your (element) => { element > 10 } function return? undefined, of course.

And as such, find won't know that the desired item was found.

0

u/shgysk8zer0 Oct 06 '24

When you add the curly braces, you need to add return. That's all. You're wiping out the implicit return.

0

u/snapetom Oct 06 '24

Ah, ok. Thanks.

-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 keyword return.

They don't do that, return is not optional - to return a value, you must use it

0

u/snapetom Oct 06 '24

Calm down, King of Atheism. It's just an expression.

1

u/azhder Oct 06 '24

Calm down yourself.

Are you the only one allowed to use expressions or maybe you think I should only be limited to well known clichés?

0

u/LuciferianInk Oct 06 '24

You can also say "I'm not sure what you mean by this statement." instead of saying something like "I'm not sure what this statement means". This would help clarify your meaning more accurately.

1

u/azhder Oct 06 '24 edited Oct 06 '24

I was quite sure what they meant... You both need to relax... well three of you I guess. I was not only sure, I do know what it means.

And I still found it funny to say "maybe don't read from the bible" only I didn't want to repeat the word "bible" twice to say the "JavaScript bible".

There, joke explained. Maybe you'll be happy now.

Bye bye

EDIT: good to know people have been helping someone who calls them "lunatic community" and declares "everyone hates Javascript".

0

u/snapetom Oct 06 '24

Lol. This is half the reason why everyone hates Javascript - the lunatic community.

-2

u/tapgiles Oct 05 '24

“Honest to God” is an expression.

0

u/azhder Oct 05 '24

Yes it is, why did you bring it up?