JavaScript ~ Bitwise NOT Operator
The bitwise NOT operator (~) inverts every bit of a 32-bit integer, which turns n into -(n + 1).
Description
The bitwise NOT operator ~ flips every bit of its operand's 32-bit integer form, which for whole numbers gives -(n + 1). Older code used ~str.indexOf(x) as a "found" check because ~-1 is 0; includes() is the clearer modern choice.
Applying it twice, ~~x, truncates a number towards zero, which was once a popular shortcut for Math.trunc(). It only works correctly for values within the 32-bit integer range, so for large numbers it gives wrong results; Math.trunc() is safer and clearer.
With bit flags, ~FLAG inverts a mask so it can be combined with & to clear that flag: flags &= ~FLAG. With a BigInt, ~ returns a BigInt.
Properties
- Operator
~- Category
- Bitwise
- Precedence
- 14 (1 is lowest, 18 is highest)
- Associativity
- right-to-left
Syntax
~xExample
Browser Support
The following information will show you the current browser support for the ~ operator. Hover over a browser icon to see the version that first introduced support for this operator.
Desktop





Tablets & Mobile





