Conditional Assignment (null coalescing)
Thought I'd share one of my favorite language features (of any language that has it): conditional assignment, sometimes called null coalescing. Here's a few versions of it:
C#:
var a = ( b??c); // a = b if b is truthy, c if b is falsey
// or:
(b ?? c).callMethod(); // nice!
javascript:
var a = b || c;
(b || c).callMethod();
python/ruby:
a = b || c
// lower precedence (i.e. only if b isn't defined, not "falsey")
a = b or c
php:
$a = b || c;
$a = b or c; // just like pythonWritten by Nick Jacob
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
projectcleverweb
309.1K
mcansky
156.1K
Filed Under
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#