Skip to main content

How to Get the Maximum of Two Numbers in JavaScript

Finding the larger of two numbers is a fundamental operation in programming, essential for comparisons, sorting, and a wide range of algorithms. JavaScript provides a simple, built-in function for this exact purpose: Math.max().

This guide will teach you how to use Math.max() to find the greater of two or more numbers, explain important alternatives like the ternary operator, and highlight the critical concept of type coercion.

The Math.max() static method is the standard and most readable way to find the largest of a set of numbers. It is a variadic function, meaning it can accept any number of arguments.

For example, we want to find the larger of two given numbers.

// Problem: Which number is greater, num1 or num2?
const num1 = 10;
const num2 = 20;

Solution:

const num1 = 10;
const num2 = 20;

const maximum = Math.max(num1, num2);

console.log(maximum); // Output: 20

// It works with any number of arguments
console.log(Math.max(10, 20, 5, 30)); // Output: 30

// It also works with negative numbers
console.log(Math.max(-10, -5)); // Output: -5
note

This method is the definitive best practice for its clarity and simplicity.

An Alternative: The Ternary Operator (? :)

For comparing just two numbers, a ternary operator is a concise alternative that is also very common. It's a shorthand for a simple if...else statement.

Logic: The expression a > b ? a : b reads as: "If a is greater than b, return a; otherwise, return b."

Solution:

const num1 = 10;
const num2 = 20;

const maximum = num1 > num2 ? num1 : num2;

console.log(maximum); // Output: 20
note

While this works well for two numbers, Math.max() is more scalable and often more readable when the logic becomes more complex.

A Note on Type Coercion

An important behavior of Math.max() is that it attempts to coerce non-numeric arguments into numbers. This can be convenient, but it can also lead to unexpected results.

// Numeric strings are converted to numbers
console.log(Math.max(5, '10')); // Output: 10

// Some non-numeric values are coerced to numbers
console.log(Math.max(true, -1)); // Output: 1 (since true becomes 1)
console.log(Math.max(null, -1)); // Output: 0 (since null becomes 0)

// If a value cannot be converted, NaN is returned
console.log(Math.max(10, 'hello')); // Output: NaN
note

Because of this behavior, you should ensure that you are only passing valid numeric values to Math.max() to avoid bugs.

How to Get the Max Value from an Array

The Math.max() function does not accept an array as an argument directly. However, you can use the spread syntax (...) to "unpack" the array's elements and pass them as individual arguments.

For example, we want to find the maximum value in an array of numbers.

const numbers = [10, 50, 30, 100, 40];

Solution:

const numbers = [10, 50, 30, 100, 40];

const maxValue = Math.max(...numbers);

console.log(maxValue); // Output: 100
note

This is the modern and standard way to find the maximum value within an array.

Conclusion

Finding the maximum of two or more numbers is a simple task in JavaScript.

  • The Math.max() method is the recommended best practice. It is clear, readable, and scalable to any number of arguments.
  • A ternary operator provides a concise alternative for comparing just two numbers.
  • To find the maximum value in an array, use the spread syntax (...) with Math.max().