3

I need to check if auth.runtime.status is equal 200, but something the auth.runtime is undifend, is there more elegant way to do it in nodejs

    if (auth.runtime) {
            if (auth.runtime.status == 200 && auth.application.status == 200) 



...

 }
}

is there a better way to write it?

lets assume I've more nested if

Beno Odr
  • 1,123
  • 1
  • 13
  • 27

1 Answers1

3

You could take the optional chaining operator ?. with a single if statement.

if (auth.runtime?.status == 200 && auth.application.status == 200) 
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • This assumes `optional chaining operator` is supported by whatever environment the OP is using, and is currently not supported in `Node.js`. – goto Jun 08 '20 at 11:24