javascript null conditional expression

javascript, null-coalescing-operator, operators

Solution

While it's considered an abusage, you can do the following:

var obj = newObject || defaultObject;

Note that if `newObject` is of any falsy value (such as `0` or an empty string), `defaultObject` will be returned as the value of `obj`. With this in mind, it may be preferred to use either the ternary operator or a standard if-statement.

var obj = ( "undefined" === typeof defaultObject ) ? defaultObject : newObject ;

Problem

Possible Duplicate: null coalescing operator for javascript? In C#, you can do this: ``` var obj = newObject ?? defaultObject; ``` That says assign `newObject` to `obj` if not null else assign `defaultObject`. How do I write this in javascript?

Original source

Related problems