Devs-Mentoring.pl - Operators JS

mail@pastecode.io avatar
unknown
javascript
a year ago
638 B
10
Indexable
Never
// Optional chaining (?.)

const mentee = {
  name: 'Casper',
  training: {
      name: 'JS' 
  },
};

const menteeName = mentee.nonexistingfield?.name;
console.log(menteeName); // Wyświetli: undefined, zamiast rzucić exception TypeError

console.log(mentee.nonexistingmethod?.()); // Wyświetli: undefined, zamiast rzucić exception TypeError


// Nullish coalescing (??)

const mentorName = null ?? 'Adam';
console.log(mentorName); // Wyświetli: 'Adam'

const menteeName = 'Agata' : undefined;
console.log(menteeName); // Wyświetli: 'Agata'

const trainingName = 'Python' ?? 'JS';
console.log(trainingName); // Wyświetli: 'Python'