javascript null safe operator code example
Example 1: conditional chaining chrome
const nameLength = db?.user?.name?.length;
const adminOption = db?.user?.validateAdminAndGetPrefs?.().option;
const optionLength = db?.user?.preferences?.[optionName].length;
const userName = usersArray?.[userIndex].name;
Example 2: optional chain operator
const adventurer = {
name: 'Alice',
cat: {
name: 'Dinah'
}
};
const dogName = adventurer.dog?.name;
console.log(dogName);
Example 3: optional chaining
const array = [1,2,3,4,5];
let arrItem = array?.[4];
console.log(arrItem);