From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../reference/global_objects/math/max/index.html | 155 +++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 files/pt-br/web/javascript/reference/global_objects/math/max/index.html (limited to 'files/pt-br/web/javascript/reference/global_objects/math/max') diff --git a/files/pt-br/web/javascript/reference/global_objects/math/max/index.html b/files/pt-br/web/javascript/reference/global_objects/math/max/index.html new file mode 100644 index 0000000000..d59b1fb583 --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/math/max/index.html @@ -0,0 +1,155 @@ +--- +title: Math.max() +slug: Web/JavaScript/Reference/Global_Objects/Math/max +tags: + - JavaScript + - Math + - Method +translation_of: Web/JavaScript/Reference/Global_Objects/Math/max +--- +
{{JSRef("Global_Objects", "Math")}}
+ +

Sumário

+ +

A função Math.max() retorna o maior de um ou mais números.

+ +

Sintaxe

+ +
Math.max([valor1[,valor2, ...]]) 
+ +

Parâmetros

+ +
+
valor1, valor2, ...
+
Números.
+
+ +

Valor de retorno

+ +

O maior dos números passados como argumentos. Se pelo menos um dos argumentos não puder ser convertido para um número {{jsxref("NaN")}} é retornado.

+ +

Descrição

+ +

Por max ser um método estático em Math, você sempre irá usá-lo da seguinte maneira Math.max(), e não como um método da classe Math que você tenha instanciado.

+ +

Se nenhum argumento for passado o resultado sempre será - {{jsxref("Global_Objects/Infinity", "Infinity")}}.

+ +

Se um dos argumentos não puder ser convertido em um número, o resultado será {{jsxref("Global_Objects/NaN", "NaN")}}.

+ +

Exemplos

+ +

Usando Math.max

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

Retornando o maior elemento de um array

+ +

{{jsxref("Array.prototype.reduce", "Array.reduce()")}} pode ser usada para encontrar o maior elemento em um vetor numérico, comparando cada valor:

+ +
var arr = [1, 2, 3];
+var max = arr.reduce(function(a, b) {
+  return Math.max(a, b);
+});
+ +

A função a seguir utiliza {{jsxref("Function.prototype.apply()")}} para encontrar o elemento de maior valor dentro do array. getMaxOfArray([1,2,3]) é equivalente a Math.max(1, 2, 3), mas você pode usar getMaxOfArray  em arrays construídos programaticamente e o ideal é utilizá-la somente em arrays com relativamente poucos elementos.

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

O novo operador spread é um modo curto de se escrever a solução com apply para retornar o maior valor de um array.

+ +
var arr = [1, 2, 3];
+var max = Math.max(...arr);
+// max: 3
+ +

Entretanto, tanto spread(...) quanto apply irão ou falhar ou retornar o resultado errado caso o array tenha muitos elementos, porque eles tentam passar o array de elementos como parâmetros de funções. Veja usando apply e funções embutidas para mais detalhes. A solução com reduce não apresenta esse problema.

+ +

Especificações

+ + + + + + + + + + + + + + + + + + + + + + + + +
EspecificaçãoStatusComentário
ECMAScript 1st Edition. Implemented in JavaScript 1.0StandardInitial definition.
{{SpecName('ES6', '#sec-15.8.2.11', 'Math.max')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-math.max', 'Math.max')}}{{Spec2('ES6')}} 
+ +

Compatibilidade de Navegadores

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}{{ CompatVersionUnknown() }}
+
+ +

Veja também

+ + -- cgit v1.2.3-54-g00ecf