• 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
  • VBA
  • About
You are here: Home / JavaScript / Using JavaScript to Get the Min Value in an Array

Using JavaScript to Get the Min Value in an Array

February 11, 2022 Leave a Comment

In JavaScript, to get the min 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 min number of an array.

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

Let’s see an example of this function below.


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

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

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

var minNum = getMinNum(numbersArray);

The variable minNum above would have value 3.

Interactive Example of Getting the Min 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="getMinNum()">Get Min Number</div>
<div id="results"></div>

We will then create an array of numbers from this input and find the min value using the getMinNum() 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 getMinNum function.

We will update the results below using the textContent property.

function getMinNum(){
  
  //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 min number
  var minNumber = numbersArray[0];
  for (var i = 0; i < numbersArray.length; i++) {
    if( numbersArray[i] < minNumber ){
      minNumber = numbersArray[i];
    }
  }

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

}

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 Min 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="getMinNum()">Get Min Number</div>
<div id="results"></div>

<script>

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

</script>

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

Other Articles You'll Also Like:

  • 1.  JavaScript Trunc with Math.trunc() Method
  • 2.  Get the First Child Element and Change it with JavaScript
  • 3.  Using JavaScript to Get the Base URL
  • 4.  Using JavaScript to Change the Font Size of a Paragraph
  • 5.  Using JavaScript to Scroll to the Top of the Page
  • 6.  Using JavaScript to Remove the First Character From a String
  • 7.  How to Split a Number into Digits in JavaScript
  • 8.  Using JavaScript to Disable a Button
  • 9.  Using JavaScript to Replace a Character at an Index
  • 10.  Using Javascript to Add Class to Element

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy