0

I have to do these checks before assing the value of object props:

if (Store != undefined) {
      if (Store.someItem != undefined) {
          if (Store.someItem['title'] != undefined) {
              title = Store.someItem['title']
          }
      }
    }

Is there short way to do this ?

For example: the last check we could do like this:

title = Store.someItem['title'] || ''

Is it possible to make all these checks in short ways. Or may be you could give advise about the whole concept and how to avoid this situation when I need to do these checks ?

yestema
  • 6,932
  • 4
  • 23
  • 29
  • what if store value is null or any other falsy value ? – Code Maniac Aug 23 '19 at 10:14
  • You don't have to nest the `if` conditions. `if (Store && Store.someItem && Store.someItem.title) title = Store.someItem.title` Or, as mentioned in the duplicate, you can create a function to get the nested properties by using a try catch. – adiga Aug 23 '19 at 10:23

0 Answers0