Quick Debug with console.log using ||

Quick Debug with console.log using ||

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 <code>||</code> before your expression. It outputs both your <code>console.log</code> and expression ๐Ÿ‘

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 evaluated ๐Ÿ‘

To learn more about the || operator, check out my previous post here


Community Input

Using the Comma operator

@phenax5: You can also use the comma operator. Separate two expressions with a comma and it will execute the first one and then the second and return with the second one.

a => (console.log(a), a + 5);

And let me break up the example so itโ€™s very clear where the 2 expressions are:

a => (
  console.log(a),
  a + 5
)

โš ๏ธ Watch your comma placement

But make sure you donโ€™t do this. I made that mistake when I first saw his example. You donโ€™t want to stick the expression inside the console.log. If you do that, your function would not return anything and nothing would be evaluated. Hence, breaking your function. So be careful with your comma placement ๐Ÿ˜…

a => (
  console.log(a, a + 5),
)

Using || with TypeScript

If youโ€™re working with TypeScript and depends on how you set it up. Using this debugging technique might give you an error and prevent your code from compiling. In that case, you can suppress the error using ts-ignore.

Using ts-ignore should no be a huge deal in this case because the console.log is only there temporarily while you debug. Once youโ€™re done, you should definitely remove it.

// @ts-ignore: Unreachable code error
() => console.log('๐Ÿค–') || expression

Thanks: @stramel89


Resources

Learn More

โ˜ž The Complete JavaScript Course 2019: Build Real Projects!

โ˜ž Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

โ˜ž JavaScript Bootcamp - Build Real World Applications

โ˜ž The Web Developer Bootcamp

โ˜ž Top 12 Javascript Tricks for Beginners

โ˜ž Top 10 JavaScript array methods you should know

โ˜ž An Introduction to Functional JavaScript

โ˜ž Building a chat widget with Go and JavaScript

โ˜ž Vuejs 2 Authentication Tutorial

Originally published at https://www.samanthaming.com

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

For World Class Web Development Services in India visit RB Genie

Do you want excellent and world class web development services for your valuable projects? Contact **RB Genie **now, we have more than 8 years experienced team of web developers, which specializes in overall web design and website development...