4.10 Exercice: max of two numbers

Here is an exercise for you: write a function that takes two numbers and returns the maximum of the two. Call the function with several test inputs to verify it works in every scenario. Pause the video, give it a try, then come back for the correction.

First implementation with if / else

Let's define a function max with two parameters. Normally we use meaningful names, but for this tiny pure function a and b are perfectly clear:

function max(a, b) {
  if (a > b) return a;
  else return b;
}

console.log(max(1, 2)); // 2
console.log(max(3, 1)); // 3
console.log(max(3, 3)); // 3

Notice the testing approach: we try the case where the second argument is larger, the case where the first is larger, and the case where both are equal. Always probe different scenarios when you write a function.

Cleaner versions

The else keyword is actually unnecessary: as soon as the first return runs, the function exits. We can simplify it further with the ternary operator learned earlier:

  • Drop else: if (a > b) return a; return b;
  • Use the ternary: return a > b ? a : b;

Both refactors are exactly equivalent to the original logic. The ternary version is the cleanest because it expresses the intent in a single line. max(5, 10) returns 10 and max(10, 5) returns 10 too — the function works correctly. See you in the next demonstration.

Summary

This lesson demonstrates how to build a `max()` function that accepts two numbers and returns the larger value. Starting with a basic if-else implementation, the instructor optimizes the code by removing the unnecessary else keyword, then refactors it further using JavaScript's ternary (conditional) operator for cleaner syntax. The lesson emphasizes the importance of testing functions with multiple input values, including edge cases where both arguments are equal.

Key points

  • Create a function called `max` with two parameters (e.g., a and b) to compare and return the maximum number
  • Start with basic if-else logic: if a > b, return a, otherwise return b
  • Optimize by removing the else keyword since the function returns immediately after the if condition evaluates to true
  • Use the ternary operator for cleaner, more concise syntax: `return a > b ? a : b`
  • Test functions with multiple test cases covering different scenarios: second argument larger, first argument larger, and both arguments equal
  • Simple variable names like a and b are acceptable when the function logic is straightforward and the context is clear

FAQ

Why can we remove the else keyword in the optimized version?

Because when the condition `a > b` is true, the function returns immediately, so any code after the if statement is skipped. Therefore, the return statement for the false case can exist without the else keyword—if the condition is false, the function simply continues to that final return statement.

What is the ternary operator and how does it work in this example?

The ternary operator is a shorthand conditional syntax with the format: `condition ? value_if_true : value_if_false`. In the max function, it reads as: `a > b ? a : b`, meaning if a is greater than b, return a; otherwise, return b. This is functionally equivalent to the if-else version but more concise.

Why is it important to test the function with different argument values?

Testing with various values ensures the function works correctly in all scenarios—when the second argument is larger, when the first is larger, and when both are equal. This validates that the logic handles edge cases and produces the expected output, which is a critical practice when writing any function.