The Bitwise NOT Operator ( ~ )

The Logical NOT Operator ( ! )

Navigation Aids -- This Page | Navigation Aids -- This Topic | Navigation Aids -- This Site | ||
---|---|---|---|---|

- precedence
- associativity
- unary operator
- bitwise Not operator
- logical Not operator
- integer operand
- boolean operand
- One's complement: negate a number and subtract one from it.

This page will show the basic rules for the JavaScript unary NOT operator sub-group. Some special situation cases are explained. And finally, some scripting examples are provided with comments.

- The unary NOT operators have the following in common:
- The unary NOT operators are single operand operators.
- They have a "right-to-left" associatively.
- They have a precedence of 14.
- These operators return a value of the same type as the operand, but leave the original operand unchanged.

- The unary bitwise NOT operator behaves as follows:
- The unary bitwise NOT operator takes a single integer operand.
- It will attempts to covert non-integer values to an integer.
- If the operator fails to covert non-integer values to an integer, it will return "-1".
- The operation of the bitwise NOT operator is as follows:
- perform type conversion if necessary,
- convert operand to 32-bit number,
- the binary number is converted into its one's complement,
- the one's complement is converted back to an integer,
- returns the integer.

- It will return "-1" if the operand is NaN.
- Synopsis: the bitwise NOT negates a number and then subtracts one from it.

- The unary logical NOT operator behaves as follows:
- The unary logical NOT operator takes a single boolean operand.
- It will attempts to covert non-boolean values to a boolean.
- If the operator fails to covert non-boolean values to a boolean, it will return false.
- When the operand is an object or number it returns false.
- If the operand is zero, null or NAN, it will return true.
- This operator reverses the boolean value of the operand and returns the boolean complement.
- By coding a double NOT ( !! ), the result will be the actual value of the operand. The double NOT performs a double reverse (for example: true becomes false which becomes true).

Expression | y = | Comment |
---|---|---|

var x = 4; y = ~x; |
-5 | unary bitwise NOT the unary bitwise NOT returns the one's complement |

var x = -4; y = ~x; |
3 | unary bitwise NOT the one's complement of -4 is 3 |

var x = true; y = !x; |
false | unary logical NOT returns boolean complement |

var x = false; y = !x; |
true | unary logical NOT returns boolean complement |

var x = 0; y = !x; |
true | unary logical NOT a zero will return true |

var x = 4; y = !x; |
false | unary logical NOT a number other than zero will return false |

var x = true; y = !!x; |
true | unary logical NOT tricky: a double NOT reverses the reversal |

var x = 0; y = !!x; |
false | unary logical NOT too tricky: a double NOT converts the zero to true, then reverses the true to false |

var x = false; y = !!!!!x; |
true | unary logical NOT NOT NOT NOT NOT go figure |