Let's say we have the function below.
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
console.log and adding the return statement. It's a lot of extra steps, right?
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
console.log once you've finished debugging.
"Logical operators are typically used with
Boolean(logical) values. When they are, they return a Boolean value. However, the
||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
console.log resolves to
falsy, the function will return the second part of it, which in our case is the
a + b . Pretty neat, right?
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! 🙏