assigning a value to a property if it does not exist
A neat way of creating a property if it does not already exist in javascript:
var foo = {};
foo.categories = foo.categories || []; //assigns empty array to the newly created 'categories' property.
foo.tags = ['javascript'];
foo.tags = foo.tags || []; //foo.tags retains the already assigned value.
Written by Guruprasad Venkatesh
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Related Tags
#javascript
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#