The ?? operator returns the first argument if it is not nullish (i.e., not null or undefined); otherwise, it returns the second argument.
let name = null; let text = “missing”; let result = name ?? text; |
The nullish operator has been supported in all browsers since March 2020.
The ?. operator returns undefined if an object is null or undefined, rather than throwing an error.
// Create an object: const car = {type:“Fiat”, model:“500”, color:“white”}; // Ask for car name: document.getElementById(“demo”).innerHTML = car?.name; |
The optional chaining operator has been supported in all browsers since March 2020.