I'm new in JS and I can't figure out what this question mark means in this line:
.querySelector('button')?.removeEventListener ..
I know what a ternary operator is, but this doesn't look like it.
Thanks
I'm new in JS and I can't figure out what this question mark means in this line:
.querySelector('button')?.removeEventListener ..
I know what a ternary operator is, but this doesn't look like it.
Thanks
It's called the Short-circuit evaluation and it's a feature of TypeScript. Not JavaScript .
When whatever before ? Is null or undefined, whatever after ?. Won't be called.
That's to avoid errors like: undefined has no property removeEventListener
Edit
I was wrong when I said it's not a feature of JavaScript. It's actually a new feature. Some browsers that are not updated to the latest don't support it yet (like mine)
Optional chaining (?.) The optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining