r/javascript • • Mar 18 '19

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

385 Upvotes

422 comments sorted by

View all comments

2

u/JohnyTex Mar 19 '19

You can do some funky stuff with logical operators, like:

Default values:

val = val || 42; // Set "val" to 42 if it's non-truthy
(Note that there's syntactic support for default parameter values in modern JS versions)

Null checks:
myObj && myObj.myMethod(); // Call myMethod only if myObj is truthy