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)
Answer from TSR on Stack OverflowWhat is the meaning of the question mark in javascript - but it is not a ternary operator? - Stack Overflow
syntax - Question mark and colon in JavaScript - Stack Overflow
How multiple question marks "?" and colons ":" in one statement are interpreted in javascript? (Conditional Operators) - Stack Overflow
New Nullish Coalescing (?? Double question mark ) Operator in Typescript & Javascript
It's very recent, it was moved to stage 4 (Finished) 5 months ago https://github.com/tc39/proposal-nullish-coalescing
More on reddit.comVideos
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
It is called the Conditional Operator (which is a ternary operator).
It has the form of: condition ? value-if-true : value-if-false
Think of the ? as "then" and : as "else".
Your code is equivalent to
if (max != 0)
hsb.s = 255 * delta / max;
else
hsb.s = 0;
Properly parenthesized for clarity, it is
hsb.s = (max != 0) ? (255 * delta / max) : 0;
meaning return either
255*delta/maxif max != 00if max == 0
Javascript is right-associative, so you 'resolve' the ternaries from right to left.
First off,
Never do this. Ever. To add to Taylor's (correct) answer, if you can't resist the siren song, then grouping things using indentation is preferable (for some definition of 'preferable').
var foo = a ?
b:
c ?
d:
e;
Which makes it a little easier to see that foo will be b if a is truthy, d if c is truthy, or e otherwise.