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?


closed as off-topic by Mathieu Guindon, Sᴀᴍ Onᴇᴌᴀ, pacmaninbw, esote, 200_success May 17 at 4:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Mathieu Guindon, Sᴀᴍ Onᴇᴌᴀ, pacmaninbw, esote, 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ If it is not set at all - does it not include undefined? \$\endgroup\$ – Avanthika May 16 at 20:46
  • 1
    \$\begingroup\$ Why have the else scope at all if it's no-op? \$\endgroup\$ – Mathieu Guindon May 16 at 21:12
  • \$\begingroup\$ @MathieuGuindon good point. lol i hadn't progressed that far in the code. \$\endgroup\$ – 1.21 gigawatts May 16 at 21:49
  • \$\begingroup\$ I'd think it's a bit early to have it peer reviewed then ;-) \$\endgroup\$ – Mathieu Guindon May 16 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 at 21:50

Browse other questions tagged or ask your own question.