Last Updated: February 25, 2016
·
327
· dvguruprasad

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.