Why does arrow function require "return" keyword if it is immediately invoked, but doesn't require "return" when it is not immediately invoked?

Viewed 14

(Javascript) Consider these three examples, where the function is identical, but in the second example it is coded as an Immediately Invoked Function Expression (I think) and returns "undefined." So in the third I added "return" and now it works as expected, returning "true." Why does the first example not require "return" but the last one does?

let fun = () => 5>2
console.log(fun());
//true


console.log(
  (() => {5 > 2})()
);
//undefined


//the same but with "return" added
console.log(
  (() => {return 5 > 2})()
);
//true
0 Answers
Related