Чтобы найти наибольшее значение в массиве с помощью JavaScript, существует несколько методов:
-
Метод 1. Использование функции Math.max() и оператора распространения:
const array = [5, 3, 9, 1, 7]; const max = Math.max(...array); console.log(max); // Output: 9 -
Метод 2. Использование метода уменьшения():
const array = [5, 3, 9, 1, 7]; const max = array.reduce((a, b) => Math.max(a, b)); console.log(max); // Output: 9 -
Метод 3. Использование цикла for:
const array = [5, 3, 9, 1, 7]; let max = array[0]; for (let i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } console.log(max); // Output: 9 -
Метод 4. Использование метода apply() с Math.max():
const array = [5, 3, 9, 1, 7]; const max = Math.max.apply(null, array); console.log(max); // Output: 9