0

In ES5 whenever I want to get some property I need to first check that it exists like this:

if (typeof floob.flib !== 'undefined') {
    // do something
}

even worse is that for nested properties you have to manually check for the existence of every property down the dotted path.

Is there a better way to do this in ES2015?

Marty
  • 39,033
  • 19
  • 93
  • 162
Duke Dougal
  • 24,359
  • 31
  • 91
  • 123
  • "In ES5 whenever I want to get some property I need to first check that it exists" --- no you don't need that. Btw, to check if the property is there - there is an `in` operator. Your check is prone to false negatives. – zerkms Jun 17 '16 at 01:25
  • @zerkms can you please expand on how the "in" operator can be used to tell me if a nested attribute exists on an object? – Duke Dougal Jun 17 '16 at 01:30
  • The same way your `typeof` would do - recursively. – zerkms Jun 17 '16 at 01:30
  • @zerkms without wanting to criticise, but I think the idea of StackOverflow is to give answers that are a full explanation rather than terse bullet points. – Duke Dougal Jun 17 '16 at 01:34
  • 1
    That is the very reason I haven't posted an answer but a comment. – zerkms Jun 17 '16 at 01:34
  • Possible duplicate: http://stackoverflow.com/questions/32139078/null-safe-property-access-in-es6-es2015 – Alex KeySmith Jul 13 '16 at 08:18

3 Answers3

2
  1. If it is just a single depth property name - you don't need typeof, you may just compare the value with undefined.

  2. Your solution is prone to false negatives: it may think there is no a property with a given name, while there is one. Example: var o = { foo: undefined };

  3. If you need to check if the path exists in the nested objects - you still need to implement recursion/loop/or use any library that does either for you.

  4. ES2015 did not bring anything new to solve this problem easier.

zerkms
  • 249,484
  • 69
  • 436
  • 539
2

If you have lodash available, you can use _.get(obj, path, defaultValue) (https://lodash.com/docs#get)

darthtrevino
  • 2,205
  • 1
  • 15
  • 17
-1

By using typeof,

typeof floob.flib === 'undefined'

equals to,

floob.flib === undefined

I assume you want to check whether floob.flib has a value, and if it does, you want to perform an operation with it.

However, in JS, there's almost simpler way to achieve this.

E.g.

if (floob.flib) {
   // 'floob.flib' is NOT 'null', 'empty string', '0', false or 'undefined'
}

This also works well if you want to assign variable with ternary ( ?: ) operators.

var str = floob.flib ? 'exists' : 'does not exist';

Or even using logical OR ( || )

var str = floob.flib || '<= either null, empty, false, 0 or undefined';

Note that unless floob.flib does not produce ReferenceError exception, the code above should work just fine.

choz
  • 17,242
  • 4
  • 53
  • 73
  • `'floob.flib' is NOT 'null', 'empty string', or 'undefined'` should include `false`. –  Jun 17 '16 at 02:27
  • @torazaburo Lol.. Yes that should include too.. with `0` also.. Thanks for pointing that out.. – choz Jun 17 '16 at 02:29