- ! 可将变量转换成 boolean 类型,null、undefined 和 空字符串 取反都为 true,其余都为 false。
- !! 常常用来做类型判断,在第一步 !(变量)之后再做逻辑取反运算
const myObj = {
a: 1,
b: 'some string',
c: [0],
d: {a: 0},
e: undefined,
f: null,
g: '',
h: NaN,
i: {},
j: [],
deleted: 'value'
};
delete myObj.deleted;
console.log(!!myObj['a']); // 1, true
console.log(!!myObj['b']); // 'some string', true
console.log(!!myObj['c']); // [0], true
console.log(!!myObj['d']); // {a: 0}, true
console.log(!!myObj['e']); // undefined, false
console.log(!!myObj['f']); // null, false
console.log(!!myObj['g']); // '', false
console.log(!!myObj['h']); // NaN, false
console.log(!!myObj['i']); // {}, true
console.log(!!myObj['j']); // [], true
console.log(!!myObj['deleted']); // false