# Logical NOT (!)

## 在此页

The logical NOT ( ``` ! ``` ) operator (logical complement, negation) takes truth to falsity and vice versa. It is typically used with ``` Boolean ``` (logical) values. When used with non-Boolean values, it returns ``` false ``` if its single operand can be converted to ``` true ``` ; otherwise, returns ``` true ``` .

```!expr
```

## 描述

If a value can be converted to ``` true ``` , the value is so-called truthy . If a value can be converted to ``` false ``` , the value is so-called falsy .

Examples of expressions that can be converted to false are:

• ``` null ``` ;
• ``` NaN ``` ;
• ``` 0 ``` ;
• empty string ( ``` "" ``` or ``` '' ``` or ``` `` ``` );
• ``` undefined ``` .

Even though the ``` ! ``` operator can be used with operands that are not Boolean values, it can still be considered a boolean operator since its return value can always be converted to a boolean primitive . To explicitly convert its return value (or any expression in general) to the corresponding boolean value, use a double NOT operator ``` Boolean ``` 构造函数。

## 范例

### Using NOT

The following code shows examples of the ``` ! ``` (logical NOT) operator.

```n1 = !true               // !t returns false
n2 = !false              // !f returns true
n3 = !''                 // !f returns true
n4 = !'Cat'              // !t returns false
```

### Double NOT ( ``` !! ``` )

It is possible to use a couple of NOT operators in series to explicitly force the conversion of any value to the corresponding boolean primitive . The conversion is based on the "truthyness" or "falsyness" of the value (see truthy and falsy ).

The same conversion can be done through the ``` Boolean ``` 函数。

```n1 = !!true                   // !!truthy returns true
n2 = !!{}                     // !!truthy returns true: any object is truthy...
n3 = !!(new Boolean(false))   // ...even Boolean objects with a false .valueOf()!
n4 = !!false                  // !!falsy returns false
n5 = !!""                     // !!falsy returns false
n6 = !!Boolean(false)         // !!falsy returns false
```

### Converting between NOTs

The following operation involving booleans :

```!!bCondition
```

is always equal to:

```bCondition
```

## 浏览器兼容性

 Desktop Mobile Server Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Chrome 1 Edge 12 Firefox 1 IE 3 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100

## 另请参阅

1. JavaScript
2. 教程：
3. 完整初学者
4. JavaScript 指南
5. 中间体
6. 高级
7. 参考：
8. 内置对象
9. 表达式 & 运算符
10. 语句 & 声明
11. 函数
12. 错误
13. 杂项