Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Expressions and operators
  5. Remainder (%)

Remainder (%)

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

Theremainder (%) operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.

Try it

console.log(13 % 5);// Expected output: 3console.log(-13 % 5);// Expected output: -3console.log(4 % 2);// Expected output: 0console.log(-4 % 2);// Expected output: -0

Syntax

js
x % y

Description

The% operator is overloaded for two types of operands: number andBigInt. It firstcoerces both operands to numeric values and tests the types of them. It performs BigInt remainder if both operands become BigInts; otherwise, it performs number remainder. ATypeError is thrown if one operand becomes a BigInt but the other becomes a number.

For the operationn % d,n is called the dividend andd is called the divisor. The operation returnsNaN if one of the operands isNaN,n is ±Infinity, or ifd is ±0. Otherwise, ifd is ±Infinity or ifn is ±0, the dividendn is returned.

When both operands are non-zero and finite, the remainderr is calculated asr := n - d * q whereq is the integer such thatr has the same sign as the dividendn while being as close to 0 as possible.

Note that while in most languages, '%' is a remainder operator, in some (e.g.,Python, Perl) it is a modulo operator. Modulo is defined ask := n - d * q whereq is the integer such thatk has the same sign as the divisord while being as close to 0 as possible. For two values of the same sign, the two are equivalent, but when the operands are of different signs, the modulo result always has the same sign as thedivisor, while the remainder has the same sign as thedividend, which can make them differ by one unit ofd. To obtain a modulo in JavaScript, in place ofn % d, use((n % d) + d) % d. In JavaScript, the modulo operation (which doesn't have a dedicated operator) is used to normalize the second operand of bitwise shift operators (<<,>>, etc.), making the offset always a positive value.

For BigInt division, aRangeError is thrown if the divisory is0n. This is because number remainder by zero returnsNaN, but BigInt has no concept ofNaN.

Examples

Remainder with positive dividend

js
13 % 5; // 31 % -2; // 11 % 2; // 12 % 3; // 25.5 % 2; // 1.53n % 2n; // 1n

Remainder with negative dividend

js
-13 % 5; // -3-1 % 2; // -1-4 % 2; // -0-3n % 2n; // -1n

Remainder with NaN

js
NaN % 2; // NaN

Remainder with Infinity

js
Infinity % 2; // NaNInfinity % 0; // NaNInfinity % Infinity; // NaN2 % Infinity; // 20 % Infinity; // 0

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-multiplicative-operators

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp