# double tilde (\~\~)

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [statement](https://lochiwei.gitbook.io/web/js/grammar/statement) ⟩ [expression](https://lochiwei.gitbook.io/web/js/grammar/statement/expr) ⟩ [operator](https://lochiwei.gitbook.io/web/js/grammar/op) ⟩ 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: [](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise/not "mention") <mark style="color:yellow;">**|**</mark> :star2: [2s-complement](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise/2s-complement "mention")
{% endhint %}

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

* :scales: [vs.-math.trunc](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise/not/vs.-math.trunc "mention")
* :heavy\_plus\_sign: [..](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise "mention") ⟩ [](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/bitwise/not "mention") - invert bits.
* :heavy\_plus\_sign: [unary](https://lochiwei.gitbook.io/web/js/grammar/op/arithmetic/unary "mention") ⟩ unary plus (+)&#x20;
* :heavy\_plus\_sign: [unary](https://lochiwei.gitbook.io/web/js/grammar/op/logical/unary "mention") ⟩ 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: 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.
