JavaScript编程中的逻辑、位运算与异步处理
1. 逻辑非运算符(!)
逻辑非运算符(!)期望一个右操作数,该操作数应为布尔值或可被转换为布尔值。只有当操作数为假时,它才返回真。在JavaScript中,逻辑非常被用作将其他类型转换为布尔值的快捷方式。以下是一些示例:
// number is coerced to a Boolean false
// NOT inverts it to true
// => true
console.log(!0);
// number is coerced to a Boolean true
// NOT inverts it to false
// => false
console.log(!1);
// number is coerced to a Boolean true
// NOT inverts it to false
// => false
console.log(!-1);
// string is coerced to a Boolean truthy *something*
// NOT inverts it to false
// => false
console.log(!'0');
// string is coerced to a Boolean truthy *something*
// NOT inverts it to false
// => false
console.log(!'1');
// this is coerced to a Boolean falsey *nothing*
// NOT invert
超级会员免费看
订阅专栏 解锁全文
3074

被折叠的 条评论
为什么被折叠?



