DEV Community

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

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

Ryan Lanciaux on October 10, 2019

Arrow functions with implicit returns are an awesome, concise way to interact with data. An example of an arrow function with implicit return ...
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
 
codefinity profile image
Manav Misra • Edited

Any suggestions on getting this approach to work in TS?

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
 
roshan_ican profile image
〽️ 𝙍𝙤𝙨𝙝𝙖𝙣

really helpful

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-