Why doesn't the optional chaining operator (?.) work with Javascript assignment?

406 Views Asked by At

When I do this:

  var resetButton = document.querySelector('.resetButton');
  resetButton?.onclick = function() {
   //...
  };

I got error: Uncaught SyntaxError: invalid assignment left-hand side.
I would like to be able to use optional chaining on every object.

1

There are 1 best solutions below

0
Gaurav Yadav On

'?.' is used when you want to access something that may be undefined(no errors)?. does not return any reference so you can't use it to assign.