剰余演算子 (%
) は、1つ目のオペランドが2つ目のオペランドで除算されたときに残った剰余を返します。 これは常に配当のサインを取ります。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
構文
Operator: var1 % var2
例
正の値の剰余
12 % 5 // 2
1 % -2 // 1
1 % 2 // 1
2 % 3 // 2
5.5 % 2 // 1.5
負の値の剰余
-12 % 5 // -2
-1 % 2 // -1
-4 % 2 // -0
NaNの剰余
NaN % 2 // NaN
仕様
ブラウザーの互換性
BCD tables only load in the browser