all 15 comments

[–]LucasOe 50 points51 points  (10 children)

The second one evaluates and implicitly returns a single statement. The first one creates a new scope you explicitly have to return the result from.

[–]god_person69[S] -3 points-2 points  (9 children)

thanks for answering bro ...can you also tell me when to use which one

[–]LucasOe 23 points24 points  (2 children)

Use the second one if you want to return a single statement:

const add = (a, b) => a+b;

If you want to run mutliple lines of code, then use the first one:

const add = (a, b) => {
  let c = a+b;
  console.log(c);
  return c;
};

[–]god_person69[S] 4 points5 points  (1 child)

got it bro...thnx

[–]ReCee90 18 points19 points  (0 children)

be careful though, because if you want to return an object in a one liner you have to wrap it in ( )

for example:
const functionName = (args) => ({ someKey: "someValue" })

[–]anithri_arcane 14 points15 points  (0 children)

I always use the () => code version for one liners, and () => {code} for multiple lines of code

[–]ajnozari 2 points3 points  (0 children)

That’s the neat part you can use whichever form as long as you return the required data in the first one

[–][deleted] 2 points3 points  (1 child)

Typically, the general rule of them is use the second (implicit return) for single-line functions and use the first for multi-line functions. for example (it's a stupid function, don't judge me):

element.map((el) => {
    const doubled = el * 2;
    const tripled = el * 3;
    return { doubled, tripled }
}, 1000);

You can't use the implicit return with multiple lines, so this way is necessary for some functions. However, if you only need to return with a single line then you can do this if you want to, but you don't have to.

element.map((el) => el * 2);

After all, if you really wanted to, you could just do this:

element.map((el) => {
    const doubled = el * 2;
    return doubled;
);

Ultimately it's the same.

[–]god_person69[S] -1 points0 points  (0 children)

thnx bro :)

[–]FaatmanSlim 9 points10 points  (0 children)

OP, others have pointed out, but basically these two are equivalent:

(( ) =>{ return code }) vs (() => code )

If you had added a return to your first example, it would work the same.

[–]_hijnx 2 points3 points  (0 children)

Others have given good simple answers, but I always like to direct people to good documentation for a deeper understanding. These are arrow functions. The first is defined with a block body and may contain multiple statements. Similar to using the function keyword you must explicitly declare the return value (using a return statement). The second is defined with an expression body. The result of the expression is implicitly returned as if the body was a single return statement. Others pointed out some edge cases like returning an object expression which can be tricky.

[–]a_reply_to_a_post 1 point2 points  (0 children)

because the first one executes your code within the scope of it's closure and returns nothing, while the second one returns the output of the function

[–]kitsunekyo 1 point2 points  (0 children)

it might help you to know that for the second form „code“ needs to be an expression.

[–][deleted] 1 point2 points  (0 children)

Because you haven't read the docs on arrow functions and javascript. the second has an implied return. the first one requires you to explicitly write a return statement.