> 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/vs.-math.trunc.md).

# (\~\~) vs. Math.trunc()

x[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
//    (almost) same as Math.trunc() = sign(x) * [|x|]
~~(7.8),            //  7
~~(-5.6),           // -5
Math.trunc(7.8),    //  7
Math.trunc(-5.6),   // -5

~~M,                // ❗ 0 (overflow: ~~(x+M) = ~~x)
Math.trunc(M),      // ⭐️ M (Math.trunc() has wider range)
```

: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="⭐️ 重點" %}
{% hint style="warning" %} <mark style="color:purple;">**\~\~**</mark>**&#x20;**<mark style="color:yellow;">**vs.**</mark>**&#x20;**<mark style="color:purple;">**Math.trunc()**</mark>：the <mark style="color:red;">**difference**</mark>

* [Math.trunc(x)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc)&#x20;
  * will try to convert x with [**"prefer-number" conversion**](/web/js/val/obj/convert/obj-to-prim.md), if it's [NaN](/web/js/val/prim/num/special/nan.md), the result <mark style="color:yellow;">**remains**</mark> <mark style="color:blue;">**NaN**</mark>.
  * has wider range. (see example above)
* <mark style="color:purple;">**bitwise not (\~)**</mark>\ <mark style="color:yellow;">**the following**</mark>**&#x20;**<mark style="color:red;">**all**</mark>**&#x20;**<mark style="color:yellow;">**converted to**</mark>**&#x20;**<mark style="color:red;">**0**</mark> before applying <mark style="color:purple;">**bitwise not (\~)**</mark> operator.
  * [null](/web/js/val/prim/null.md) <mark style="color:yellow;">**/**</mark> [undefined](/web/js/val/prim/undefined.md) <mark style="color:yellow;">**/**</mark> <mark style="color:red;">**false**</mark> <mark style="color:yellow;">**/**</mark> <mark style="color:orange;">**`""`**</mark> (<mark style="color:yellow;">**empty string**</mark>) <mark style="color:yellow;">**/**</mark> [NaN](/web/js/val/prim/num/special/nan.md) <mark style="color:yellow;">**/**</mark> [Infinity](/web/js/val/prim/num/special/infinity.md)
  * <mark style="color:yellow;">**non-numeric**</mark> [String](/web/js/val/prim/str.md) <mark style="color:yellow;">**/**</mark> <mark style="color:yellow;">**non-numeric**</mark> [Array](/web/js/val/builtin/arr.md)&#x20;
  * <mark style="color:yellow;">**other**</mark> [object](/web/js/val/obj.md) <mark style="color:yellow;">**(**</mark><mark style="color:red;">**except**</mark> [Date](/web/js/val/builtin/date.md)<mark style="color:yellow;">**)**</mark>
    {% endhint %}
    {% 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="👥 相關" %}

* [bitwise operator](/web/js/grammar/op/arithmetic/bitwise.md) ⟩ [bitwise not (\~)](/web/js/grammar/op/arithmetic/bitwise/not.md) - invert bits.
* [unary arithmetic operator](/web/js/grammar/op/arithmetic/unary.md) ⟩ unary plus (+)
* [unary logical operator](/web/js/grammar/op/logical/unary.md) ⟩ logical not (!)
  {% 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 %}
