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?
Asked
Active
Viewed 226 times
2
-
1in the block {}, you need a return keyword – MUHAMMAD ILYAS Jan 10 '21 at 15:30
2 Answers
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
-
Hello @soimon Hope you are doing good !!! How is life Mate :) – Imran Rafiq Rather Jan 10 '21 at 15:36
-
2@soimon, thanks for the answer. i voted it up, but J.Langer was to first to answer (also a good answer), therefore i have approved his answer. thanks!!! – Eitanos30 Jan 10 '21 at 15:39