How to Use Optional Chaining (?.) in JavaScript
Optional chaining, ?., reads a property only when the value before it exists. When that value is null or undefined, you get undefined instead of an error.
- Programming
- JavaScript
Optional chaining, ?., reads a property only when the value before it exists. When that value is null or undefined, you get undefined instead of an error.
a && b gives back b when a is truthy, and a when a is falsy, so it can read a property only when its object exists.
value ?? fallback gives back the fallback only when value is null or undefined.
The ternary operator picks one of two values: condition ? valueIfTrue : valueIfFalse.
Compare a value with === null to find out whether it’s empty on purpose, and with !== null to check that it holds something.
Put ! in front of a value to get true when the value is missing or empty, and false when it’s there.