Arrow functions can differ in function bodies (Thanks Robbie). The concise function body can only consist of a single expression which is evaluated and implicitly returned. The conventional block function body requires the return
keyword or it will return void
.
example1 = () => 1 + 1;
example2 = () => {
const result = 1 + 1;
return result;
};
example3 = () => {
const result = 1 + 1;
};
example1()
has a concise body and will implicitly return the result of the expression 2
.
example2()
has a block body and does explicitly return 2
.
example3()
has a block body and no explicit return, therefore it returns void
.
Note that the normal braces ()
around a concise function body are required if you want to return an object literal:
example = () => ({some: 'object'});