Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

Javascript’s Math.max() function

We will write a simple Javascript page to explore the Math.max() function. Given one or more numbers as inputs, Math.max() is a Javascript function that returns the largest input or NaN if any one of the inputs is not a number. Begin with a basic webpage:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Javascript Math.max() function</title>
</head>

<body>
  <script>
  </script>
</body>

</html>

In the above HTML page you see the basic elements of the page like the head element containing the title (“Javascript Math.max() function”) and a body with an empty script tag. The Javascript code goes inside these tags.

Using Math.max() in Javascript

Javascript's Math max() function

Here is some simple code to compute the maximum of a set of numbers.

<script>
  document.write("Find the max of (10,20,-30)")
  document.write('<BR>')
  document.write(Math.max(10,20,-30))
</script>

The output is:

Find the max of (10,20,-30)
20

In general you can provide a more elaborate range of numbers:

<script>
  document.write("Find the max of (10,20,-30,0,5.4,-4.6,2367)")
  document.write('<BR>')
  document.write(Math.max(10,20,-30,0,5.4,-4.6,2367))
</script>

The output is, as expected:

Find the max of (10,20,-30,0,5.4,-4.6,2367)
2367

Math.max() with no parameters passed

If we do not pass any arguments to Math.max(), like so:

<script>
  document.write("Find the max of ()")
  document.write('<BR>')
  document.write(Math.max())
</script>

The output is:

Find the max of ()
-Infinity

Note that Math.max() returns “-Infinity” if no arguments are given.

Math.max() with non-numeric arguments

If we use non-numeric arguments with Math.max() like so:

<script>
  document.write("Find the max of (10,'Hello')")
  document.write('<BR>')
  document.write(Math.max(10,'Hello'))
</script>

we will get the output:

Find the max of (10,'Hello')
NaN

Note that Math.max() returns NaN if at least one of its inputs is not numeric.

Math.max() with an array

Let us try to find the maximum value in an integer array. Here is an attempt:

<script>
  var ages = [11,12,13,12,11,15]
  document.write("Find the max of student ages")
  document.write('<BR>')
  document.write(Math.max(ages))
</script>

The output is:

Find the max of student ages
NaN

Wow - what happened? Note that Math.max() takes individual numbers as input whereas you are passing an array. You should use spread notation to achieve your goal:

<script>
  var ages = [11,12,13,12,11,15]
  document.write("Find the max of student ages")
  document.write('<BR>')
  document.write(Math.max(...ages))
</script>

The output is, as expected:

Find the max of student ages
15

Would you like to learn more Javascript? Checkout our blog post on how you can use Javascript’s Array shift() method.

Want to learn Javascript with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2025. All rights reserved.