Does return is added implicitly in a callback function?

Viewed 106

I have a code (attached screenshot) that in one place it is written explictiy reutrn inside a callback, and in another one it isn't. I'm trying to understand what is the reason for it? In my opinion return statement should be added also in the first one. Am i wrong?

enter image description here

2 Answers

There is a return in the first one. Read this about arrow functions

"(...) If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword" - https://www.w3schools.com/js/js_arrow_function.asp

What you're seeing is a property of the arrow function: if the braces are omitted the result of the following statement is returned automatically.

input => output is the same as input => { return output; }.

Note that this behavior differs from regular functions, as these two functions do not both return a result:

function a(input) { return 1 };
function b(input) { 1 };
a() // 1
b() // undefined
Related