JavaScript: Find the Largest Number in an Array

Problem Statement:

Write a JavaScript function to find the largest number in an array.

Solution Explanation:

  • We use the Math.max() function along with the spread operator (...) to find the largest number.

JavaScript Code:

function findLargest(arr) {
    return Math.max(...arr);
}

// Test cases
console.log(findLargest([10, 20, 5, 30])); // Output: 30
console.log(findLargest([-1, -5, -10, 0])); // Output: 0

Leave a Reply