Movatterモバイル変換


[0]ホーム

URL:


  1. 개발자를 위한 웹 기술
  2. JavaScript
  3. JavaScript 참고서
  4. 표준 내장 객체
  5. Math
  6. Math.max()

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

View in EnglishAlways switch to English

Math.max()

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.max() 정적 메서드는 매개변수로 주어진 숫자 중 가장 큰 수를 반환하거나,매개변수가 없을 경우 -Infinity를 반환합니다.

시도해 보기

console.log(Math.max(1, 3, 2));// Expected output: 3console.log(Math.max(-1, -3, -2));// Expected output: -1const array1 = [1, 3, 2];console.log(Math.max(...array1));// Expected output: 3

구문

js
Math.max()Math.max(value1)Math.max(value1, value2)Math.max(value1, value2, /* …, */ valueN)

매개변수

value1, …,valueN

가장 큰 값을 선택하고 반환할 0개 이상의 숫자입니다.

반환 값

주어진 숫자 중 가장 큰 숫자를 반환합니다. 만약 인수 중 하나라도 숫자로 변환한 값이NaN이라면NaN로 반환합니다. 매개변수가 없을 경우 -Infinity를 반환합니다.

설명

max()Math의 정적 메서드이기 때문에 만든Math객체의 메서드가 아닌 항상Math.max()로 사용해야합니다. (Math는 생성자가 아닙니다).

Math.max.length의 값이 2 인데, 이는 이 메서드가 최소 2개의 매개변수를 받도록 설계되었음을 암시합니다.

예제

Math.max()함수 사용하기

js
Math.max(10, 20); // 20Math.max(-10, -20); // -10Math.max(-10, 20); // 20

배열의 최대값 가져오기

Array.prototype.reduce()는 각 값을 비교하여 숫자 배열의 최대 요소를 찾는 데 사용할 수 있습니다.

js
const arr = [1, 2, 3];const max = arr.reduce((a, b) => Math.max(a, b), -Infinity);

다음 함수는Function.prototype.apply()를 사용하여 배열의 최대값을 가져옵니다.getMaxOfArray([1, 2, 3])Math.max(1, 2, 3)와 동일하지만, 프로그래밍 방식으로 배열을 생성하기 위해getMaxOfArray()를 사용할 수 있습니다. 이 함수는 상대적으로 요소가 적은 배열에만 사용해야 합니다.

js
function getMaxOfArray(numArray) {  return Math.max.apply(null, numArray);}

전개 구문은 배열의 최대값을 구하기 위한apply 솔루션을 짧게 작성하는 방법입니다.

js
const arr = [1, 2, 3];const max = Math.max(...arr);

그러나 전개 구문(...)와apply는 모두 배열 요소를 함수 매개변수로 전달하려고 하기 때문에 배열에 요소가 너무 많으면 실패하거나잘못된 결과를 반환합니다. 자세한 내용은apply와 내장 함수 사용하기를 참조하세요.reduce 솔루션에는 이 문제가 없습니다.

명세서

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

브라우저 호환성

같이 보기

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp