1.使用in判断对象中是否包含某属性
const person = { name: 'intic', salary: 200 };
console.log('salary' in person); // true
console.log('age' in person); // false
2. ?? 空值合并操作符
const foo = null ?? 'Hello';
console.log(foo); // 'Hello'
const bar = 'Not null' ?? 'Hello';
console.log(bar); // 'Not null'
3. 可选链==?.==
const book = { id:1, title: 'Title', author: null };
// 通常情况下,你会这样做
console.log(book.author.age) // throws error
console.log(book.author && book.author.age); // null
const person = {
firstName: '前端',
lastName: '小智',
printName: function () {
return `${this.firstName} ${this.lastName}`;
},
};
console.log(person.printName()); // '前端 小智'
console.log(persone.doesNotExist?.()); // undefined
4. !! 运算符可用于将表达式的结果快速转换为布尔值(true或false):
const greeting = 'Hello there!';
console.log(!!greeting) // true
const noGreeting = '';
console.log(!!noGreeting); // false