Skip to content

Find the smallest number in Array JavaScript | Example code

  • by

Use the Math min() method to Find the smallest number in the Array using JavaScript.

Math.min(...arr)

Example Find the smallest number in Array JavaScript

HTML example code.

Using Math.min(…arr)

It is the easiest way to return the smallest value of an array is to use the Spread Operator on Math.min() function.

<!DOCTYPE html>
<html>
<body>

  <script>

    const arr = [14, 58, 20, 77, 66, 2, 42, 67, 42, 4]
    const min = Math.min(...arr)
    console.log(min)
  </script>

</body>
</html>

Output:

Find the smallest number in Array JavaScript

Use case for Array.prototype.reduce:

const arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
const min = arr.reduce((a, b) => Math.min(a, b))
console.log(min)

Use classic iteration:

var arr,
  i,
  l,
  min

arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
min = Number.POSITIVE_INFINITY
for (i = 0, l = arr.length; i < l; i++) {
  min = Math.min(min, arr[i])
}
console.log(min)

Modern

const arr = [14, 58, 20, 77, 66, 82, 42, 67, 42, 4]
let min = Number.POSITIVE_INFINITY
for (const value of arr) {
  min = Math.min(min, value)
}
console.log(min)

Do comment if you have any doubts or suggestions on this JS Array code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

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