Search code examples
javascriptoptional-chaining

Why is it necessary to use the dot operator before the optional chaining - before a function?


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.


Solution

  • 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