This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

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 ⁨2015년 7월⁩.

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 객체(Math는 생성자가 아닙니다)의 메서드 대신 언제나 Math.abs()를 사용해야 합니다.

예제

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® 2026 Language Specification
# sec-math.abs

브라우저 호환성

같이 보기