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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
While it’s considered an abusage, you can do the following:
Note that if
newObjectis of any falsy value (such as0or an empty string),defaultObjectwill be returned as the value ofobj. With this in mind, it may be preferred to use either the ternary operator or a standard if-statement.