> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise/not/double-tilde.md).

# double tilde (\~\~)

[JS](/web/js.md) ⟩ [statement](/web/js/grammar/statement.md) ⟩ [expression](/web/js/grammar/statement/expr.md) ⟩ [operator](/web/js/grammar/op.md) ⟩ double tilde (\~\~)

{% hint style="success" %} <mark style="color:orange;">**nearest**</mark>**&#x20;**<mark style="color:yellow;">**integer**</mark>**&#x20;**<mark style="color:orange;">**towards**</mark> <mark style="color:red;">**0**</mark><mark style="color:yellow;">**.**</mark>

```javascript
// ⭐️ ~~(x) : nearest integer towards 0
~~(-5.5)        // -5
~~( 5.5)        //  5

+undefined      // ⭐️ NaN
~~undefined     // ⭐️ 0

// similar operators
!!x        // any -> bool
+x         // any -> number
```

:bulb: it's not really an operator, it's just <mark style="color:yellow;">`~~x === ~(~x)`</mark>.

:point\_right: [bitwise not (\~)](/web/js/grammar/op/arithmetic/bitwise/not.md) <mark style="color:yellow;">**|**</mark> :star2: [2's complement](/web/js/grammar/op/arithmetic/bitwise/2s-complement.md)
{% endhint %}

{% tabs %}
{% tab title="👥 相關" %}

* :scales: [(\~\~) vs. Math.trunc()](/web/js/grammar/op/arithmetic/bitwise/not/vs.-math.trunc.md)
* :heavy\_plus\_sign: [bitwise operator](/web/js/grammar/op/arithmetic/bitwise.md) ⟩ [bitwise not (\~)](/web/js/grammar/op/arithmetic/bitwise/not.md) - invert bits.
* :heavy\_plus\_sign: [unary arithmetic operator](/web/js/grammar/op/arithmetic/unary.md) ⟩ unary plus (+)&#x20;
* :heavy\_plus\_sign: [unary logical operator](/web/js/grammar/op/logical/unary.md) ⟩ logical not (!)
  {% endtab %}

{% tab title="💈範例" %}
{% embed url="<https://codesandbox.io/embed/compare-math-trunc-bi2gh?fontsize=14&hidenavigation=1&theme=dark>" %}
"\~\~" vs. Math.trunc()
{% endembed %}

{% hint style="warning" %}
注意：使用 [JSON.stringify()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) 將函數轉換成字串時，會轉成 "**undefined**"❗️
{% endhint %}
{% endtab %}

{% tab title="📗 參考" %}

* [ ] CodeWars ⟩ [Adding Big Numbers](https://www.codewars.com/kata/525f4206b73515bffb000b21)
  {% endtab %}

{% tab title="📘 手冊" %}

* [Bitwise NOT (\~)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT)
* [Math.trunc()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc) - same as <mark style="color:purple;">**`~~`**</mark> (with wider range)
  {% endtab %}

{% tab title="🗣 討論" %}

* SOF ⟩ [What does \~\~ ("double tilde") do in Javascript?](https://stackoverflow.com/a/4055675)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise/not/double-tilde.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
