aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/math/max/index.html
blob: a3e994c734fef9f49fd3925cdc7fce54d078f621 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
---
title: Math.max()
slug: Web/JavaScript/Reference/Global_Objects/Math/max
tags:
  - JavaScript
  - Math
  - Method
  - Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Math/max
---
<div>{{JSRef}}</div>

<p><strong><code>Math.max()</code></strong> 函式會回傳零或多個數字中的最大值。</p>

<div>{{EmbedInteractiveExample("pages/js/math-max.html")}}</div>



<h2 id="語法">語法</h2>

<pre class="syntaxbox"><code>Math.max([<var>value1</var>[, <var>value2</var>[, ...]]])</code></pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>value1, value2, ...</code></dt>
 <dd>Numbers.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>The largest of the given numbers. If at least one of the arguments cannot be converted to a number, {{jsxref("NaN")}} is returned.</p>

<h2 id="說明">說明</h2>

<p>Because <code>max()</code> is a static method of <code>Math</code>, you always use it as <code>Math.max()</code>, rather than as a method of a <code>Math</code> object you created (<code>Math</code> is not a constructor).</p>

<p>If no arguments are given, the result is -{{jsxref("Infinity")}}.</p>

<p>If at least one of arguments cannot be converted to a number, the result is {{jsxref("NaN")}}.</p>

<h2 id="範例">範例</h2>

<h3 id="Using_Math.max()">Using <code>Math.max()</code></h3>

<pre class="brush: js">Math.max(10, 20);   //  20
Math.max(-10, -20); // -10
Math.max(-10, 20);  //  20
</pre>

<h4 id="Getting_the_maximum_element_of_an_array">Getting the maximum element of an array</h4>

<p>{{jsxref("Array.prototype.reduce", "Array.reduce()")}} can be used to find the maximum element in a numeric array, by comparing each value:</p>

<pre class="brush: js">var arr = [1,2,3];
var max = arr.reduce(function(a, b) {
    return Math.max(a, b);
}, -Infinity);
</pre>

<p>The following function uses {{jsxref("Function.prototype.apply()")}} to get the maximum of an array. <code>getMaxOfArray([1, 2, 3])</code> is equivalent to <code>Math.max(1, 2, 3)</code>, but you can use <code>getMaxOfArray()</code> on programmatically constructed arrays. This should only be used for arrays with relatively few elements.</p>

<pre class="brush: js">function getMaxOfArray(numArray) {
  return Math.max.apply(null, numArray);
}</pre>

<p>The new <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator">spread operator</a> is a shorter way of writing the <code>apply</code> solution to get the maximum of an array:</p>

<pre class="brush: js">var arr = [1, 2, 3];
var max = Math.max(...arr);
</pre>

<p>However, both spread (<code>...</code>) and <code>apply</code> will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters. See <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply#Using_apply_and_built-in_functions">Using <code>apply</code> and built-in functions</a> for more details. The <code>reduce</code> solution does not have this problem.</p>

<h2 id="規範">規範</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.0.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.8.2.11', 'Math.max')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-math.max', 'Math.max')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-math.max', 'Math.max')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="瀏覽器相容性">瀏覽器相容性</h2>

<p>{{Compat("javascript.builtins.Math.max")}}</p>

<h2 id="參見">參見</h2>

<ul>
 <li>{{jsxref("Math.min()")}}</li>
</ul>