return of es6

Asked 2 years ago, Updated 2 years ago, 127 views

module.exports={
     add:(value1,value2) =>
     return value1+value2
 }

When I wrote it like this, I got an error, but I don't know why.

I turned off the return, but I don't know why it shouldn't be returned.

javascript es6

2022-09-30 18:22

2 Answers

ref: Arrow function|MDN

If you want to use return in the statements part of the arrow function, you may need to enclose it with {}.

module.exports={
    add:(value1,value2)=>{return value1+value2}
}

Also, you can omit the return and write as follows.

module.exports={
    add:(value1,value2) =>value1+value2
}


2022-09-30 18:22

{} can only be omitted if the code you want to write is a single 式expression 」.The expression can be the right side of the assignment statement or the parameter of the function.

If you want to write a sentence, including return, you must write {}.

As for why this is the case, reading TC39Wiki may be simply imitating the lambda grammar of C# (not clearly written).


2022-09-30 18:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.