0

why the braces around the arrow function makes it to yield undefined when the next case turns true.

    function greater(n){
    return (m)=>{m>n};
     }
   >greater(10)(11)
   <undefined

    function great(n){
    return m=>m>n;
    } 
   <great(10)(11)
   >true
  • 8
    Possible duplicate of [How is () => {...} different from () =>](https://stackoverflow.com/questions/52334319/how-is-different-from) and [When should I use return in es6 Arrow Functions?](https://stackoverflow.com/questions/28889450) and [Arrow function without curly braces](https://stackoverflow.com/questions/39629962) – adiga Apr 19 '19 at 13:44
  • 1
    Because when you use block move you have to explicit type `return`. – Lux Apr 19 '19 at 13:45
  • 1
    This answer in particular: https://stackoverflow.com/questions/52334319/how-is-different-from#answer-52334964 – Sergiu Paraschiv Apr 19 '19 at 13:45

1 Answers1

1

Arrow functions have an implicit return, which means if you don't wrap their body with {}, then you don't need to use the return keyword. Your function returns undefined because you're not providing a return value. You need to either remove the curly braces or add a return keyword:

function greater(n){
    return (m)=>{return m>n};
}

One last thing to note, if you want to implicitly return an object, the curly braces will be treated as the function body and so nothing will be returned. In that case, you'd need to wrap the object in parentheses:

const wrong = () => { test: true }
const right = () => ({ test: true })
SoKeT
  • 570
  • 5
  • 16