JavaScript Code Styling Best Practices — Arrow Functions and Spacing

John Au-Yeung
The Startup
Published in
4 min readMay 15, 2020

--

Photo by Michael Bourgault on Unsplash

JavaScript is a very forgiving language. It’s easy to write code that runs but has mistakes in it.

In this article, we’ll look at how to return items from arrow functions implicitly and spacing and indentation to make our code easy to read and clean.

Return Things with Arrow Functions Correctly

With JavaScript arrow functions, we can return things implicitly. If the arrow function is one line, then the last thing that’s on the arrow function is returned implicitly.

If the arrow function is more than one line long, then expressions can be returned implicitly by wrapper the object with parentheses.

For instance, the correct way to return something in a one-line arrow function is the following:

const double = x => x * 2;

In the code above, returned x * 2 given then parameter x , where x is a number.

Therefore, we return whatever x is multiplied by 2.

If our expression is more than one line long, then we have to wrap it around parentheses in order to return it.

For instance, if we want to return an object, then we have to write the following code:

--

--