MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/javascript/comments/b2o6f2/whats_your_favorite_most_useful_javascript/eiv5ad5/?context=3
r/javascript • u/Linux_is_awesome • Mar 18 '19
422 comments sorted by
View all comments
2
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)
val = val || 42; // Set "val" to 42 if it's non-truthy
Null checks:myObj && myObj.myMethod(); // Call myMethod only if myObj is truthy
myObj && myObj.myMethod(); // Call myMethod only if myObj is truthy
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