Quick Debug using || with console.log

Samantha Ming
DailyJS
Published in
3 min readApr 1, 2019

--

CodeTidbit by SamanthaMing.com

It’s always a pain to debug 1-line arrow function with a console.log. Why? b/c we need to convert it to a multi-line first. No more! Just use || before your expression. It outputs both your console.log and expression 👍

And clean up is a breeze! No more messy re-conversion back to a 1-line. Just remove your console.log. And you're done 😆

// ✅
() => console.log('🤖') || expression
// ❌
() => {
console.log('🤖')
return expression
}

Example

Let’s take a look at a simple example of how this would work:

const numbers = [1,2,3];numbers.map(number => number * 2);// ✅ Debug quickly by prepending with `||`
numbers.map(number => console.log(number) || number * 2);
// ❌ No need to expand it to multi line
numbers.map(number => {
console.log(number);
return number * 2;
});

How does the || work?

Often times we think the || operator is only used in conditional statements. However, you can also think of it as a selector operator. It will always evaluate one of the 2 expressions.

Because console.log always return undefined, which is a falsy value. The second expression will always be…

--

--

Samantha Ming
DailyJS

Frontend Developer sharing weekly JS, HTML, CSS code tidbits🔥 Discover them all on samanthaming.com 💛