剰余代入演算子 (%=)
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
剰余代入演算子 (%=
) は、2 つのオペランドで剰余の計算を行い、結果を左オペランドに代入します。
試してみましょう
let a = 3;
console.log((a %= 2));
// 予想される結果: 1
console.log((a %= 0));
// 予想される結果: NaN
console.log((a %= "hello"));
// 予想される結果: NaN
構文
js
x %= y
解説
x %= y
は x = x % y
と同等ですが、式 x
が一度だけ評価される点が異なります。
例
>剰余代入の使用
js
let bar = 5;
bar %= 2; // 1
bar %= "foo"; // NaN
bar %= 0; // NaN
let foo = 3n;
foo %= 2n; // 1n
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification> # sec-assignment-operators> |
ブラウザーの互換性
Loading…