Devs-Mentoring.pl - Operators JS

mail@pastecode.io avatarunknown
javascript
a month ago
638 B
7
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'