Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Math.max()

Fungsi Math.max() mengembalikan nilai terbesar dari zero atau lebih besar.

Sintaks

Math.max([value1[, value2[, ...]]])

Parameter

value1, value2, ...
Nomor.

Deskripsi

Karena max() merupakan method static dari Math, anda perlu menggunakan Math.max(), daripada sebagai method dari objek Math yang anda buat (Math bukan constructor).

Jika tidak diberi argumen, hasilnya akan -Infinity.

Jika setidaknya satu argumen tidak dapat dikonversi ke angka, maka hasilnya NaN.

Contoh

Penggunaan Math.max()

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

Pada contoh menggunakan fungsi Function.prototype.apply() untuk mencari nilai elemen maksimum pada sebuah numeric array. getMaxOfArray([1, 2, 3]) sama halnya dengan Math.max(1, 2, 3), tapi anda bisa menggunakan getMaxOfArray() pada programmatikal pembuatan array untuk semua ukuran.

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

Atau dengan spread operator baru, mencari nilai terbesar menjadi lebih mudah.

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

Spesifikasi

Spesifikasi Status Comment
ECMAScript 1st Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.0.
ECMAScript 5.1 (ECMA-262)
The definition of 'Math.max' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Math.max' in that specification.
Standard  
ECMAScript 2017 Draft (ECMA-262)
The definition of 'Math.max' in that specification.
Draft  

Kompabilitas Browser

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

Lihat juga

Tag Dokumen dan Kontributor

 Kontributor untuk laman ini: rmsubekti
 Terakhir diperbarui oleh: rmsubekti,