• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Get the Max Value in an Array

Using JavaScript to Get the Max Value in an Array

February 11, 2022 Leave a Comment

In JavaScript, to get the max value in an array we can do this by using a for loop with an if conditional statement. Here is a function to get the max number of an array.

function getMaxNum(){
  var maxNumber = arrayOfNumber[0];
  for (var i = 0; i < arrayOfNumber.length; i++) {
    if( arrayOfNumber[i] > maxNumber ){
      maxNumber = arrayOfNumber[i];
    }
  }
  return maxNumber;
}

Let’s see an example of this function below.


In the example below, we will have an array of numbers. We will use our getMaxNum() function we created above to find the maximum number and return it.

var numbersArray = [4,3,5,8,14,67,56,23];

function getMaxNum(){
  var maxNumber = numbersArray[0];
  for (var i = 0; i < numbersArray.length; i++) {
    if( numbersArray[i] > maxNumber ){
      maxNumber = numbersArray[i];
    }
  }
  return maxNumber;
}

var maxNum = getMaxNum(numbersArray);

The variable maxNum above would have value 67.

Interactive Example of Getting the Max Value of an Array

Below we will provide code to let the user input as many numbers as they want separated by a comma(,).

Here is the simple HTML set up:

<p>Type as many numbers as you want separated by a comma.</p>
<p>If the numbers are not separated by a comma only, this example will not work.</p>
<input id="userArr" type="text" placeholder="1,2,3">
<div id="click-me" onclick="getMaxNum()">Get Max Number</div>
<div id="results"></div>

We will then create an array of numbers from this input and find the max value using the getMaxNum() function we created above.

To create an array from the string of numbers the user provides, we will use the split() method along with the Array map() method. We will add this code to our getMaxNum function.

We will update the results below using the textContent property..

function getMaxNum(){
  
  //Get the user input
  var userInput = document.getElementById("userArr").value;

  //Convert user input to an array of numbers
  var numbersArray = userInput.split(',').map(Number);

  //Find the max number
  var maxNumber = numbersArray[0];
  for (var i = 0; i < numbersArray.length; i++) {
    if( numbersArray[i] > maxNumber ){
      maxNumber = numbersArray[i];
    }
  }

  //Display the max number
  document.getElementById("results").textContent = "The max number is: " + maxNumber;

}

The final code and output for this example is below:

Code Output:

Type as many numbers as you want separated by a comma.

If the numbers are not separated by a comma only, this example will not work.

Get Max Number

Full Code:

<p>Type as many numbers as you want separated by a comma.</p>
<p>If the numbers are not separated by a comma only, this example will not work.</p>
<input id="userArr" type="text" placeholder="1,2,3">
<div id="click-me" onclick="getMaxNum()">Get Max Number</div>
<div id="results"></div>

<script>

function getMaxNum(){
  var userInput = document.getElementById("userArr").value;
  var numbersArray = userInput.split(',').map(Number);
  var maxNumber = numbersArray[0];
  for (var i = 0; i < numbersArray.length; i++) {
    if( numbersArray[i] > maxNumber ){
      maxNumber = numbersArray[i];
    }
  }
  document.getElementById("results").textContent = "The max number is: " + maxNumber;
}

</script>

Hopefully this article has helped you to understand how to get the max value in an array using JavaScript.

Other Articles You'll Also Like:

  • 1.  JavaScript atan2 – Find Arctangent of the Quotient of Two Numbers
  • 2.  Using JavaScript to Get the Current Month
  • 3.  JavaScript atan – Find Arctangent and Inverse Tangent of Number
  • 4.  JavaScript acos – Find Arccosine and Inverse Cosine of Number
  • 5.  Using JavaScript to Wait 5 Seconds Before Executing Code
  • 6.  Using JavaScript to Create an Empty Array
  • 7.  How to Create a JavaScript Count Up Timer
  • 8.  Add Days to a Date Using JavaScript
  • 9.  How to Check if a Number is Even or Odd in JavaScript
  • 10.  Convert a Set to Array in JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy