Math.abs()

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.abs() 靜態方法會回傳一個數字的絕對值。

嘗試一下

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

console.log(difference(3, 5));
// Expected output: 2

console.log(difference(5, 3));
// Expected output: 2

console.log(difference(1.23456, 7.89012));
// Expected output: 6.6555599999999995

語法

js
Math.abs(x)

參數

x

一個數字。

回傳值

x 的絕對值。如果 x 是負數或 -0,則回傳它的相反數 -x(非負數)。否則,回傳 x 本身。因此,結果必定是正數或 0

描述

由於 abs()Math 的靜態方法,你必須使用 Math.abs(),而不是在你所建立的 Math 物件上呼叫此方法(Math 並不是建構子)。

範例

使用 Math.abs()

js
Math.abs(-Infinity); // Infinity
Math.abs(-1); // 1
Math.abs(-0); // 0
Math.abs(0); // 0
Math.abs(1); // 1
Math.abs(Infinity); // 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® 2025 Language Specification
# sec-math.abs

瀏覽器相容性

BCD tables only load in the browser

參見