While it is considered abuse, you can do the following:
var obj = newObject || defaultObject;
Note that if newObject
has any falsy value (for example, 0
or an empty string), defaultObject
will be returned as obj
value. With this in mind, it may be preferable to use either the ternary operator or the standard if statement.
var obj = ( "undefined" === typeof defaultObject ) ? defaultObject : newObject ;
source share