2

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

Eitanos30
  • 1,331
  • 11
  • 19

2 Answers2

3

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

J. Langer
  • 262
  • 3
  • 17
3

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
soimon
  • 2,400
  • 1
  • 15
  • 16