JavaScript Math max() method - JavaScript Tutorial

JavaScript Math max() method

BLUF: This tutorial on JavaScript Math max() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Math max() method

Understanding JavaScript Math max() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The max method in JavaScript's Math object evaluates the provided numbers and returns the largest value among them.

Syntax

The syntax for the max method is expressed as follows:

Example

Math.max(num1,num2,....,numN)

Parameter

num1,num2,....,numN - Numbers to be compared..

Return

A maximum value of the given numbers.

JavaScript Math max method example

In this section, we will explore the max function by examining several examples.

Example 1

Let's examine a straightforward example that demonstrates how to display the highest value among a set of provided numbers.

Example

<script>

document.writeln(Math.max(22,34,12,15)+"<br>"); 

document.writeln(Math.max(-10,-24,-12,-20));

</script>

Output:

Output

34

-10

Example 2

Consider the following example that demonstrates how to output the highest value from the provided array.

Example

<script>

var arr=[13,15,12,9]

document.writeln(Math.max(...arr)) 

</script>

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience