DEV Community

Discussion on: What does return do in JavaScript

Collapse
 
mellen profile image
Matt Ellen

Fun fact about return statements. They get semicolon insertion if they're by themselves. That is:

return 3;
Enter fullscreen mode Exit fullscreen mode

is not the same as

return
3;
Enter fullscreen mode Exit fullscreen mode

While it doesn't usually come up, because who would write code like that?, it is a difference between javascript and say, c or c++ where both return styles would return 3.

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Agreed, but it's more likely to write code like this in React...

   return 
          <div>
              <span style={someStyle}>{content}</span>
         </div>
Enter fullscreen mode Exit fullscreen mode

Which is why most editors will insert a parenthesis....

   return ( 
          <div>
              <span style={someStyle}>{content}</span>
         </div>
    )
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mellen profile image
Matt Ellen

Parenthesis insertion to combat semicolon insertion! Amazing 😁