The optional chaining ?. syntax has three forms:
obj?.prop– returnsobj.propifobjexists, otherwiseundefined.obj?.[prop]– returnsobj[prop]ifobjexists, otherwiseundefined.obj.method?.()– callsobj.method()ifobj.methodexists, otherwise returnsundefined.
let user = {}; // user has no address
alert( user?.address?.street ); // undefined (no error)
------------------------------------
let userGuest = {};
userGuest.admin?.(); // nothing (no such method)
https://javascript.info/optional-chaining
No comments:
Post a Comment