Kodeclik Blog
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.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2024. All rights reserved.
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:
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.
Here is some simple code to compute the maximum of a set of numbers.
The output is:
In general you can provide a more elaborate range of numbers:
The output is, as expected:
If we do not pass any arguments to Math.max(), like so:
The output is:
Note that Math.max() returns “-Infinity” if no arguments are given.
If we use non-numeric arguments with Math.max() like so:
we will get the output:
Note that Math.max() returns NaN if at least one of its inputs is not numeric.
Let us try to find the maximum value in an integer array. Here is an attempt:
The output is:
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:
The output is, as expected:
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.
<!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>
<script>
document.write("Find the max of (10,20,-30)")
document.write('<BR>')
document.write(Math.max(10,20,-30))
</script>
Find the max of (10,20,-30)
20
<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>
Find the max of (10,20,-30,0,5.4,-4.6,2367)
2367
<script>
document.write("Find the max of ()")
document.write('<BR>')
document.write(Math.max())
</script>
Find the max of ()
-Infinity
<script>
document.write("Find the max of (10,'Hello')")
document.write('<BR>')
document.write(Math.max(10,'Hello'))
</script>
Find the max of (10,'Hello')
NaN
<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>
Find the max of student ages
NaN
<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>
Find the max of student ages
15