-2

Instead of doing ...

if (typeof some !== 'undefined' && some.thing.variable === 'someValue')

... is it acceptable to just do the following?

if (some && some.thing.variable === 'someValue')
Grateful
  • 9,685
  • 10
  • 45
  • 77
  • 2
    Just use `someVariable === 'someValue'`. – Tushar Oct 25 '16 at 07:36
  • 2
    Do you care about other falsey values? If so - no, otherwise - yes. – VLAZ Oct 25 '16 at 07:36
  • Actually, yeah Tushar is right - you don't really need to check for undefined first if you want exact match anyway - by definition the value can't be `undefined` if it's `"someValue"` – VLAZ Oct 25 '16 at 07:37
  • Best practice: be explicit whenever possible (the first one is more explicit) – Yang Oct 25 '16 at 07:38
  • 1
    Possible duplicate of [All falsey values in JavaScript](http://stackoverflow.com/questions/19839952/all-falsey-values-in-javascript) – Deblaton Jean-Philippe Oct 25 '16 at 07:38
  • Your update doesn't change anything. Unless you do `typeof some !== "undefined" && some.variable == "someValue"` in which case it would make more sense and then the second form `some && some.variable == "someValue"` is perfectly acceptable and idiomatic. – VLAZ Oct 25 '16 at 07:39
  • So you are saying that it makes sense to have `if(some)` instead of `if(typeof some)`. But when does it make sense to have `typeof`? – Grateful Oct 25 '16 at 07:45
  • When you want to check for `undefined`. Or if it's another type, like number or string, but in this context, it's just `undefined`. – VLAZ Oct 25 '16 at 07:47
  • Thanks. Does a variable default to null or undefined? – Grateful Oct 25 '16 at 07:49
  • 1
    @Grateful well if you just do `var a;` it will be `undefined`. It follows logically, as the value is...not surprisingly, _not_ defined. You could set the value explicitly to `undefined`, as well but, in general, `undefined` means "I have not assigned a value yet, so it's empty" and `null` means "I have set a value myself and that value is empty", so when assigning "non-values" yourself, you should prefer `null`. Also be aware that `if (a)` will NOT pass for `0` or `""` yet, they may be valid values, e.g., `totalAmount = 0` http://dorey.github.io/JavaScript-Equality-Table/ – VLAZ Oct 25 '16 at 09:14

1 Answers1

-2

Yes, this is acceptable in a private function. Just make sure you have declared the variable some to avoid a ReferenceError.

Rationale: Since there are a few downvotes I think I need to give some context to why I think this is acceptable. I mention "private function" because in a private function the assumption is that we know which objects have which properties. So - regardless of style - the second write-up is an acceptable alternative to the original code snippet only if some is indeed the object as advertised.

The function in question would need to have the variable declared and should not be accessible from outside. Some start these variables with an underscore:

// Declare variable
var _some;
Bruno
  • 898
  • 14
  • 17
  • if `obj` is undefined, you will still get an error. If you have a deeper nesting, your code becomes unreadable. The best would be : `if (obj && obj.p) {}`. But anyway, it's just code styles that can only create debates, SO is not for that kind of answer. – Deblaton Jean-Philippe Oct 25 '16 at 08:11
  • Oops! True that. Fixed. For readability you can wrap it in a function. But my point is that checking for `undefined` is pointless if you are not sure the property exist, as it will error either way. – Bruno Oct 25 '16 at 22:26