Skip to main content


Deno standard library
Go to Latest
function maxWith
import { maxWith } from "";

Returns the first element having the largest value according to the provided comparator or undefined if there are no elements.

The comparator is expected to work exactly like one passed to Array.sort, which means that comparator(a, b) should return a negative number if a < b, a positive number if a > b and 0 if a == b.


import { maxWith } from "";
import { assertEquals } from "";

const people = ["Kim", "Anna", "John", "Arthur"];
const largestName = maxWith(people, (a, b) => a.length - b.length);

assertEquals(largestName, "Arthur");


array: readonly T[]
comparator: (a: T, b: T) => number


T | undefined