Does Javascript support the short ternary (rather, variation of) as in PHP?

Dan Lugg picture Dan Lugg · Sep 14, 2011 · Viewed 7.2k times · Source

I've become fond of PHP's support for the "short ternary", omitting the second expression:

// PHP

$foo = 'hello';
$bar = '';

echo $foo ?: 'world'; // hello
echo $bar ?: 'world'; // world

Does Javascript support any sort of syntax like this? I've tried ?: resulting in a syntax error. I'm aware of boolean short circuits, but that's not feasible for what I'm currently doing; that being:

// Javascript

var data = {
    key: value ?: 'default'
};

Any suggestions? (I could wrap it in an immediately invoked anonymous function, but that seems silly)

Answer

Leonid picture Leonid · Sep 14, 2011
var data = {
    key: value || 'default'
};