Math.sign()

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.

Math.sign() 静的メソッドは 1 または -1 を返し、引数として渡された値の符号を表します。入力値が 0 または -0 ならば、そのまま返します。

試してみましょう

console.log(Math.sign(3));
// Expected output: 1

console.log(Math.sign(-3));
// Expected output: -1

console.log(Math.sign(0));
// Expected output: 0

console.log(Math.sign("-3"));
// Expected output: -1

構文

js
Math.sign(x)

引数

x

数値です。

返値

与えられた引数の符号を表す数値です。

  • 引数が正の値の場合は、 1 を返します。
  • 引数が負の値の場合は、 -1 を返します。
  • 引数が正のゼロの場合は、 0 を返します。
  • 引数が負のゼロの場合は、 -0 を返します。
  • それ以外は NaN を返します。

解説

sign()Math の静的メソッドなので、常に Math.sign() として使用し、自分で Math オブジェクトを生成してそのメソッドとして使用しないでください。 (Math にはコンストラクターがありません)。

Math.sign() の使用

js
Math.sign(3); // 1
Math.sign(-3); // -1
Math.sign("-3"); // -1
Math.sign(0); // 0
Math.sign(-0); // -0
Math.sign(NaN); // NaN
Math.sign("foo"); // NaN
Math.sign(); // NaN

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-math.sign

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
sign

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報