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 July 2015.

Math.max() 関数は、入力引数として与えられた 0 個以上の数値のうち最大の数を返します。引数がなかった場合は -Infinity を返します。

試してみましょう

console.log(Math.max(1, 3, 2));
// Expected output: 3

console.log(Math.max(-1, -3, -2));
// Expected output: -1

const array1 = [1, 3, 2];

console.log(Math.max(...array1));
// Expected output: 3

構文

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

引数

value1, value2, … , valueN

最大値を選択して返すための、 0 個以上の数値です。

返値

与えられた数のうちの最大の値です。何れかの引数が NaN であるか、それに変換された場合は NaN を返します。引数が与えられなかった場合は -Infinity を返します。

解説

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

Math.max.length は 2 であり、最低でも 2 つの引数を処理するよう設計されていることを示唆しています。

Math.max() の使用

js
Math.max(10, 20); // 20
Math.max(-10, -20); // -10
Math.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® 2025 Language Specification
# sec-math.max

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
max

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報