# double tilde (\~\~)

[JS](../../../../..) ⟩ [statement](../../../../statement) ⟩ [expression](../../../../statement/expr) ⟩ [operator](../../..) ⟩ 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: [](<> "mention") <mark style="color:yellow;">**|**</mark> :star2: [2s-complement](../2s-complement "mention")
{% endhint %}

{% tabs %}
{% tab title="👥 相關" %}
* :scales: [vs.-math.trunc](vs.-math.trunc "mention")
* :heavy\_plus\_sign: [..](.. "mention") ⟩ [](<> "mention") - invert bits.
* :heavy\_plus\_sign: [unary](../../unary "mention") ⟩ unary plus (+)&#x20;
* :heavy\_plus\_sign: [unary](../../../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 %}
