単項マイナス (-)
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.
単項マイナス演算子 (-
) はオペランドの前に置かれ、符号を反転します。
試してみましょう
const x = 4; const y = -x; console.log(y); // Expected output: -4 const a = "4"; const b = -a; console.log(b); // Expected output: -4
構文
js
-x;
例
数値の符号を反転
js
const x = 3; const y = -x; // y = -3 // x = 3
数値以外の符号を反転
単項マイナス演算子は、数値でないものを数値に変換することができます。
js
const x = "4"; const y = -x; // y = -4
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-unary-minus-operator |