Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
Math.abs() 靜態方法會回傳一個數字的絕對值。
一個數字。
x 的絕對值。如果 x 是負數或 -0,則回傳它的相反數 -x(非負數)。否則,回傳 x 本身。因此,結果必定是正數或 0。
由於 abs() 是 Math 的靜態方法,你必須使用 Math.abs(),而不是在你所建立的 Math 物件上呼叫此方法(Math 並不是建構子)。
Math.abs() 會將參數強制轉型為數字。無法轉型的值將變為 NaN,因此 Math.abs() 也會回傳 NaN。
| ECMAScript® 2027 Language Specification # sec-math.abs |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2026年2月2日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.