Friday, January 22, 2021

Optional chaining '?.' javascript

 The optional chaining ?. syntax has three forms:

  1. obj?.prop – returns obj.prop if obj exists, otherwise undefined.
  2. obj?.[prop] – returns obj[prop] if obj exists, otherwise undefined.
  3. obj.method?.() – calls obj.method() if obj.method exists, otherwise returns undefined.

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

javascript Filter/index off

 var family = [{"name":"Jack",  "age": 26},               {"name":"Jill",  "age"...