0

So this is the pic

restaurant is the object and order is a function in that object

so as you can see why do I have to use the dot operator after trying to do the optional chaining ?

I tried to exclude it but then it shows error.

Any help is appreciated.

Regexes
  • 95
  • 8

2 Answers2

2

The reason is that ? and ?. are two different things. Only the latter is Option chaining, so if you remove the period, you have a start of a ternary operation.

Also note that The optional chaining ?. is not an operator, but a special syntax construct src

Daniel
  • 28,755
  • 15
  • 87
  • 134
1

That's how the syntax was specified.

Why? Because without the dot, the ? is ambiguous and could the the begin of a conditional operator expression (… ? (…) : …) instead. To make parsing easier (not having to lookahead and find a matching :), the dot that immediately follows the question mark is required.

Bergi
  • 572,313
  • 128
  • 898
  • 1,281