-1

I'm trying to understand this piece of code:

!isLoading && !QRCodes?.length ? (.....

What is the role of ? in !QRCodes?.length? Wouldn't be the statement false anyway, when there's no QRCodes?

Zin Yosrim
  • 1,602
  • 1
  • 22
  • 40
  • [Optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) – Jaromanda X Jul 29 '22 at 10:29
  • 1
    "Wouldn't be the statement false anyway?" - nope, it would be "Uncaught Type Error: cannot read 'length' property of undefined/null". – mbojko Jul 29 '22 at 10:29
  • 1
    The question mark is used to check if `QRCodes` is undefined before accessing the length property, so to prevent a possible exception. Take a look at this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining – Davide Bulbarelli Jul 29 '22 at 10:30

1 Answers1

-1

!isLoading means if not loading and the QRCodes length is 0 display the thing after the (... the question mark in QRCodes?.length means that get length if exists