r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

130

u/Earhacker Mar 18 '19 edited Mar 19 '19

Not exactly a one-liner, but my neatest quick trick: If you have a one-liner arrow function, but you want to log out the parameter, you don't need to give it curly braces and an explicit return.

Go from this:

const addFortyTwo = number => number + 42

To this:

const addFortyTwo = number => console.log(number) || number + 42

It works because console.log returns undefined, which is falsey, so the expression evaluates to the other side of the || 'or' operator, but only after running the console.log. Neat!

41

u/seiyria Mar 19 '19

You could also use the , operator for this, like so:

const add42 = n => (console.log(n), number + 42);

Although, since you need parens around it, it's a bit more obnoxious.

14

u/natziel Mar 19 '19

This is especially handy with React. Imagine you have a component like the one below, but you want to log the props

const Example = (props) => (
  <Foo ...props />
)

Dead easy to add or remove the log without having to mess with the implementation of the component

1

u/UNN_Rickenbacker Mar 19 '19

Can you give a concrete example?

1

u/mastermah Mar 21 '19

const Example = (props) => ( console.log(props), <Foo ...props /> )

I would assume

1

u/swyx Mar 19 '19

brendan eich even recommends this and i dont understand it. its obvious that || is easier to use than comma operator plus parens.

8

u/seiyria Mar 19 '19

I did say it's more obnoxious to use the comma operator. I do not recommend it, I just wanted to offer an alternative for those that might like the syntax better. There are some cases where the comma operator is more useful / easier to use, but I can't say I recommend using it in general as it creates code that's easier to forget about, which might create some unintentional side effects.

5

u/FormerGameDev Mar 19 '19

Many people have rules preventing the use of bool operators as flow control. With good reason.

3

u/NeatBeluga Mar 19 '19

Like a ternary operator?

1

u/seiyria Mar 19 '19

I also agree with this, and I don't particularly like that syntax either. Usually it's something you can do for dev real quick and delete it before you commit, though.

8

u/msluther Mar 19 '19

I use this all the time but as a condition on a breakpoint. Because it’s falsey, it won’t stop at the breakpoint, but you get console log statements without altering the code. Super handy for dealing with more time sensitive code or where it’s too much work to alter the source.

5

u/[deleted] Mar 19 '19

Oh my goodness why have I never thought of this? Thank you for saving me 2 seconds times a billion

3

u/Dextro_PT Mar 19 '19

Due to the same fact, if you want to log a variable at any given point, you can just add a conditional breakpoint with console.log(variable) and it'll log without stopping execution

2

u/ScientificBeastMode strongly typed comments Mar 19 '19

Oh wow this is brilliant!

2

u/joopez1 Mar 19 '19

thank you for this

2

u/jseego Mar 19 '19

That is cool

1

u/evenisto Mar 19 '19

Holy shit, done are the days of adding curly braces just to log shit. Thank you so much.

1

u/bethegood Mar 19 '19

This one wins for me. Thank you!

1

u/ssjskipp Mar 19 '19

I usually write a tap helper or if I'm using lodash it has it.

const tap = (fn, tapper) => (...args) => { tapper(...args); return fn(...args); };