0

I am dealing with the old codebase again and was just curious, can this line be left so or can be improved?

if (data.COUNTRY === undefined || data.COUNTRY === null || data.COUNTRY.length === 0) {}

Ren
  • 944
  • 1
  • 13
  • 26
  • 3
    `data.COUNTRY == null || data.COUNTRY.length === 0` – Felix Kling Mar 24 '20 at 12:20
  • 1
    One thing is that `data.COUNTRY == null` does exactly the same thing as comparing with `===` to both `null` and `undefined`. – Pointy Mar 24 '20 at 12:20
  • If, if it exists, it won't be falsey, then yes. Otherwise, not really, though you could use `.includes` if you wanted to be DRY – CertainPerformance Mar 24 '20 at 12:20
  • try `if(data.country)` – Muhammad Murad Haider Mar 24 '20 at 12:21
  • 1
    That line is fine. It is short and very clear. If you absolutely want to change it, you could use the suggestion of Felix or something like `!data.COUNTRY && typeof data.COUNTRY !== 'number'`. I guess it depends on the exact context to decide what to use. Note that just using `!data.COUNTRY` changes the behaviour of the code. For example, `0` is falsy but your code does not include it in your condition. – str Mar 24 '20 at 12:25
  • For a more declarative, just `if(!Boolean(data.COUNTRY) || !Boolean(data.COUNTRY.length)) { ... }` – Marco Sanfilippo Mar 24 '20 at 12:47

0 Answers0