Reference · Lists

max

signature
max(list | null) -> any

What it does

The largest item of a list, or null when the list is empty. Numbers compare as numbers (ints, floats and decimals together), strings by code point. It takes one list, not several values: max([a, b]), not max(a, b).

Examples

print(max([3, 1, 2]))print(max([1, dec("2.75"), 2]))print(max(["pear", "apple", "fig"]))let people = [{ name: "Ann", age: 30 }, { name: "Bo", age: 25 }]print(max(map(people, (p) => p.age)))
output
32.75pear30

Edge cases

print(max([]), max(null), max([]) ?? 0)print(max(["b", "B", "a"]), max(["10", "9"]))print(max([2, 2.0]), max([2.0, 2]))print(max([5]), max([dec("1.0"), 1]))
output
null null 0b 92 2.05 1.0

Errors

An argument that is not a list or null is a runtime error, not an error value. Two items that do not order are a runtime error too: a string with a number, null, booleans, lists, maps, NaN, or a decimal with a float (convert one side first), in the browser as on the server.

this one fails
print(max([null, 1]))
output
line 1: `max` cannot compare int with null
this one fails
print(max([true, false]))
output
line 1: `max` cannot compare bool with bool
this one fails
print(max([dec("1"), 1.5]))
output
line 1: `max` cannot compare float with dec: a dec is exact and a float is not, so convert one with dec(x) or float(x)

Chaining

In a pipeline max takes the list and needs no arguments: xs | map(f) | max. For the item with the largest key rather than the key itself, sort by it and take the last.

let words = ["pear", "fig", "apple"]print(words | map(len) | max)print(words | sort_by(len) | last)print([] | max ?? 0)
output
5apple0

See also

min, sort, sort_by, last, sum, vec.argmax

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On max(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo