DEV Community

Cover image for Quick tip: How to console.log in implicitly returned arrow functions
Ryan Lanciaux
Ryan Lanciaux

Posted on

Quick tip: How to console.log in implicitly returned arrow functions

Arrow functions with implicit returns are an awesome, concise way to interact with data.

An example of an arrow function with implicit return

const sum = (a, b) => a + b;
Enter fullscreen mode Exit fullscreen mode

Where an arrow function without an implicit return would look like this:

const sum = (a, b) => { return a + b; }
Enter fullscreen mode Exit fullscreen mode

Very similar but in the first example, the return value of the function is inferred where in the latter, we're specifying the return statement of the function.

Logging in a standard function / arrow function is pretty straight-forward

const sum = (a, b) => {
  console.log('HERE!');
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

But how do we accomplish this same thing in an arrow function with implicit return? Many times, developers convert the function with implicit return to a standard function but this isn't necessary.

A potential solution

We can take advantage of the fact that console.log is evaluated as falsy. This means that if we ran

if(console.log('someStatement')) { 
  // ... 
} else {
 ...
} 
Enter fullscreen mode Exit fullscreen mode

We would encounter our else block every time. Using this knowledge, we can now update our arrow function with logging as follows:

const sum = (a, b) => console.log('A:', a, 'B:', b) || a + b;
Enter fullscreen mode Exit fullscreen mode

Top comments (11)

Collapse
Β 
lucis profile image
Lucis β€’

Really nice!

And also, using an object into console.log prints both the variable name and its value, like: console.log({a, b})

Collapse
Β 
ryanlanciaux profile image
Ryan Lanciaux β€’

Thank you! This is a really great point!

Collapse
Β 
steventcramer profile image
Steven T. Cramer β€’

Ryan just informed me that TypeScript knows console.log is void function so you need to cast it to any.

(console.log({a,b}) as any) || a + b;

Thanks again.

Collapse
Β 
ajinspiro profile image
Arun Kumar β€’

That's wicked smart

Collapse
Β 
steventcramer profile image
Steven T. Cramer β€’

Great tip Ryan!

Collapse
Β 
thehanna profile image
Brian Hanna β€’

That's super clever! I log in functions like this all the time, but I add braces and convert to an explicit return

Collapse
Β 
tusharborole profile image
Tushar Borole β€’

Thats nice share

Collapse
Β 
roundem profile image
JASON ROUNDTREE β€’ β€’ Edited

Very helpful but why does console.log evaluate to falsy and why does it still log when false?

Collapse
Β 
codefinity profile image
Manav Misra β€’ β€’ Edited

Any suggestions on getting this approach to work in TS?

Collapse
Β 
chrismarx profile image
chrismarx β€’

For Typescript, I think this is easier (a,b) => (console.log(a), a+b)
The comma will only return the last expression-

Collapse
Β 
roshan_ican profile image
〽️ 𝙍𝙀𝙨𝙝𝙖𝙣 β€’

really helpful