Find the largest element of an array js code example
Example 1: find biggest number in javascript array
var peopleData = [
{ name: "Paul", height: 180, age: 21 },
{ name: "Johnny", height: 198, age: 43 },
{ name: "Brad", height: 172, age: 49 },
{ name: "Dwayne", height: 166, age: 15 }
];
//Find biggest height number
var maxHeight = 0;
for (var i = 0; i < heights.length; i++) {
if (peopleData[i].height > maxHeight) {
maxHeight = peopleData[i].height;
//if you console.log(maxHeight); you should get 198
}
}
Example 2: get largest number in array javascript
const array1 = [1, 3, 2];
Math.max(...array1);
// expected output: 3