> 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/binary/remainder.md).

# remainder (%)

remainder \`r = a % b\`, \`r\` has the same sign as \`a\`.

[JS](/web/js.md) ⟩ [operator](/web/js/grammar/op.md) ⟩ [arithmetic](/web/js/grammar/op/arithmetic.md) ⟩ remainder (%)

{% hint style="success" %} <mark style="color:yellow;">**`a`**</mark><mark style="color:purple;">**`%`**</mark><mark style="color:yellow;">**`b`**</mark> returns&#x20;

* [<mark style="color:blue;">**NaN**</mark>](/web/js/val/prim/num/special/nan.md),  <mark style="color:red;">**if**</mark> <mark style="color:yellow;">**one**</mark>**&#x20;of the&#x20;**<mark style="color:yellow;">**operands**</mark> is [<mark style="color:blue;">**NaN**</mark>](/web/js/val/prim/num/special/nan.md) <mark style="color:yellow;">**/**</mark><mark style="color:yellow;">**`a`**</mark> = <mark style="color:yellow;">**±**</mark>[<mark style="color:blue;">**Infinity**</mark>](/web/js/val/prim/num/special/infinity.md) <mark style="color:yellow;">**/**</mark> <mark style="color:yellow;">**`b`**</mark> = <mark style="color:yellow;">**±**</mark><mark style="color:red;">**0**</mark>.&#x20;
* <mark style="color:yellow;">**`a`**</mark>,  <mark style="color:red;">**if**</mark> <mark style="color:yellow;">**`a`**</mark> = <mark style="color:yellow;">**±**</mark><mark style="color:red;">**0**</mark> <mark style="color:yellow;">**/**</mark><mark style="color:yellow;">**`b`**</mark> = <mark style="color:yellow;">**±**</mark>[<mark style="color:blue;">**Infinity**</mark>](/web/js/val/prim/num/special/infinity.md).&#x20;
* <mark style="color:yellow;">**`r`**</mark><mark style="color:blue;">**`=`**</mark><mark style="color:yellow;">**`a`**</mark><mark style="color:blue;">**`-`**</mark><mark style="color:yellow;">**`b`**</mark><mark style="color:blue;">**`*`**</mark><mark style="color:yellow;">**`q`**</mark> where：
  * <mark style="color:yellow;">**`q`**</mark> is the integer such that：
  * <mark style="color:yellow;">**`r`**</mark> has the <mark style="color:red;">**same**</mark>**&#x20;**<mark style="color:yellow;">**sign**</mark> as<mark style="color:yellow;">**`a`**</mark>, while being <mark style="color:yellow;">**as close to**</mark>**&#x20;**<mark style="color:red;">**0**</mark> as possible.

```javascript
// ⭐️ `a` decides the sign of the remainder
 5 % 2    //  1
-5 % 2    // -1   
```

:point\_right: [table of operators](/web/js/grammar/op/table-of-operators.md)**,** [remainder vs. modulo vs. mod](/web/js/grammar/op/arithmetic/binary/remainder/remainder-vs.-modulo-vs.-mod.md)
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="warning" %}
:star: <mark style="color:purple;">**remainder (%)**</mark> and [<mark style="color:blue;">**modulo**</mark>](/web/js/grammar/op/arithmetic/binary/remainder/modulo.md) <mark style="color:yellow;">**are**</mark>**&#x20;**<mark style="color:red;">**different**</mark>:exclamation:\
:point\_right: [remainder vs. modulo vs. mod](/web/js/grammar/op/arithmetic/binary/remainder/remainder-vs.-modulo-vs.-mod.md)
{% endhint %}

{% hint style="warning" %} <mark style="color:purple;">**`% operator`**</mark>&#x20;

* <mark style="color:yellow;">**works**</mark> for [floating-point](/web/js/val/prim/num/floating-point.md) numbers too.
  {% endhint %}
  {% endtab %}

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

* [modulo(a, b)](/web/js/grammar/op/arithmetic/binary/remainder/modulo.md)
* [mod(a, b)](/web/js/grammar/op/arithmetic/binary/remainder/mod.md)
  {% endtab %}

{% tab title="💈範例" %}

* replit： [modulo(a,b) vs. mod(a,b)](https://replit.com/@pegasusroe/modulo-moda-b#index.js)

```javascript
// ⭐️ remainder (a % b)
//
// ┌─── `a` decides the "sign" ⭐️
// │
   5 %  3,    //  2
   5 % -3,    //  2
  -5 %  3,    // -2
  -5 % -3,    // -2

// modulo(a, b)
//          ┌─── `b` decides the "sign" ⭐️
//          │ 
modulo( 5,  3),    //  2
modulo( 5, -3),    // -1
modulo(-5,  3),    //  1
modulo(-5, -3),    // -2

modulo(6.4, 2.3),     //  1.8000000000000007

// mod(a, b) (in math)
//                    ┌─── always >= 0 ⭐️
//                    │ 
mod( 5,  3),       // 2
mod( 5, -3),       // 2
mod(-5,  3),       // 1
mod(-5, -3),       // 1
```

{% endtab %}

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

* [ ] [JavaScript: The Definitive Guide](/web/master/ref/javascript-the-definitive-guide.md) ⟩ 4.8&#x20;
  {% endtab %}

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

* [Remainder (%)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder)
  {% endtab %}
  {% endtabs %}
