Skip to content
the poor coder

Find Largest Number by function by javascript

Poor Man
Published
Reading time
2 min read
On this page

Find the Largest Number by Function in JavaScript#

JavaScript provides several ways to find the largest number in an array or a set of numbers. Here, we will discuss some ways to find the largest number by function in JavaScript.

Method 1: Using Math.max() Function#

The easiest and most straightforward way to find the largest number in an array or a set of numbers is to use the Math.max() function.

Text
function findLargestNumber(numbers) {
  return Math.max(...numbers);
}

let numbers = [1, 5, 3, 7, 2];
let largestNumber = findLargestNumber(numbers);

console.log(largestNumber); // Output: 7

In the above example, the findLargestNumber() function takes an array of numbers as an argument and returns the largest number using the Math.max() function. The spread operator (...) is used to pass the array elements as individual arguments to the Math.max() function.

Method 2: Using a For Loop#

If you don't want to use the Math.max() function, you can also find the largest number using a for loop.

Text
function findLargestNumber(numbers) {
  let largestNumber = numbers[0];
  
  for (let i = 1; i < numbers.length; i++) {
    if (numbers[i] > largestNumber) {
      largestNumber = numbers[i];
    }
  }
  
  return largestNumber;
}

let numbers = [1, 5, 3, 7, 2];
let largestNumber = findLargestNumber(numbers);

console.log(largestNumber); // Output: 7

In the above example, the findLargestNumber() function takes an array of numbers as an argument and iterates over the elements using a for loop. It initializes the largestNumber variable with the first element of the array and compares it with each subsequent element. If the current element is greater than the largestNumber, it updates the largestNumber variable with the current element.

Method 3: Using the reduce() Method#

You can also use the reduce() method to find the largest number in an array.

Text
function findLargestNumber(numbers) {
  return numbers.reduce((acc, curr) => acc > curr ? acc : curr);
}

let numbers = [1, 5, 3, 7, 2];
let largestNumber = findLargestNumber(numbers);

console.log(largestNumber); // Output: 7

In the above example, the findLargestNumber() function takes an array of numbers as an argument and calls the reduce() method on it. The reduce() method takes a callback function that compares each element with an accumulator variable and returns the largest one.

These are some ways to find the largest number by function in JavaScript. You can choose any method that suits your needs and preferences.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC