Debugging Simple Arrow Functions More Efficiently

Photo by Markus Spiske on Unsplash

Let's say we have the function below.

The function

It works fine, but we seem to encounter a weird bug from time to time. So what's the first step we take in debugging? Yes, you got it, we will start by logging the function parameters.

In order to do that though, we would need to refactor our function to something like the one below. Including the curly brackets, writing the and adding the return statement. It's a lot of extra steps, right?

Debugging the function, first try

But it does not have to be! If we just want to log the parameters, the version of the function below is a quick way to do it. We no longer have to convert our single line arrow function into a multi-line one.

Just don't forget to remove the once you've finished debugging.

Debugging the function, second try

It seems a little hacky though and why does this even work? The statement is valid because of how logical operators work in JavaScript.

"Logical operators are typically used with (logical) values. When they are, they return a Boolean value. However, the and operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they will return a non-Boolean value" — MDN web docs

Because resolves to, the function will return the second part of it, which in our case is the . Pretty neat, right?

Final words

Discovering this small trick was game-changer when it comes to debugging arrow functions more efficiently. What do you think of this technique? Did you know about this before? Let's continue the discussion in the comments section.

Thanks for reading, and subscribe to do not miss out on any of my future posts! 🙏

Robert is a full-stack web developer, currently working at Relatable. He loves to inspire people, explore new places, read great books, take inspiring pictures and learn new stuff all the time.

Follow him on LinkedIn, Instagram, GitHub, Goodreads or here, on Medium.

Building for the web, traveling the world with hand luggage, inspiring people and learning constantly. Follow my journey 🙂

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store