Math.abs() - JavaScript | MDN

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月.

Math.abs() は静的メソッドで、数値の絶対値を返します。

試してみましょう

function difference(a, b) {
  return Math.abs(a - b);
}

console.log(difference(3, 5));
// 予想される結果: 2

console.log(difference(5, 3));
// 予想される結果: 2

console.log(difference(1.23456, 7.89012));
// 予想される結果: 6.6555599999999995

構文

引数

x

数値です。

返値

x の絶対値です。x が負または -0 の場合は、その反対の数である -x (非負の値)を返します。それ以外の場合、x 自体を返します。したがって、返値は常に正の値または 0 となります。

解説

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

Math.abs() の使用

js

Math.abs(-Infinity); // 無限大
Math.abs(-1); // 1
Math.abs(-0); // 0
Math.abs(0); // 0
Math.abs(1); // 1
Math.abs(Infinity); // 無限大

引数の変換

Math.abs()引数を数値に変換します。変換できない値は NaN となり、Math.abs()NaN を返します。

js

Math.abs("-1"); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs(""); // 0
Math.abs([]); // 0
Math.abs([2]); // 2
Math.abs([1, 2]); // NaN
Math.abs({}); // NaN
Math.abs("string"); // NaN
Math.abs(); // NaN

仕様書

Specification
ECMAScript® 2026 Language Specification
# sec-math.abs

ブラウザーの互換性

関連情報

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.