Devs-Mentoring.pl - Operators JS
unknown
javascript
2 years ago
638 B
18
Indexable
// 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'
Editor is loading...