I have a case where I want to apply a format if a property is true or if it is not set at all. Here is what I have:

var item = {};
item.name = "Option 1";

if (item.addTags==true || item.addTags==null) {
   // user wants to add tags. if not set then default is to add tags
else {
   // property is set and is not true. do not add tags  

Is there any potential issues with doing it this way?

  • 1
    \$\begingroup\$ If it is not set at all - does it not include undefined? \$\endgroup\$ – Avanthika May 16 '19 at 20:46
  • 1
    \$\begingroup\$ Why have the else scope at all if it's no-op? \$\endgroup\$ – Mathieu Guindon May 16 '19 at 21:12
  • \$\begingroup\$ @MathieuGuindon good point. lol i hadn't progressed that far in the code. \$\endgroup\$ – 1.21 gigawatts May 16 '19 at 21:49
  • \$\begingroup\$ I'd think it's a bit early to have it peer reviewed then ;-) \$\endgroup\$ – Mathieu Guindon May 16 '19 at 21:50
  • \$\begingroup\$ @Avanthika in JavaScript if the value is undefined the loose equality would catch it when using value==null. \$\endgroup\$ – 1.21 gigawatts May 16 '19 at 21:50

Browse other questions tagged or ask your own question.