Languages
[Edit]
EN

TypeScript - max value in array

3 points
Created by:
Imaan-Morin
1009

In this article, we would like to show you how to find maximum value in array in TypeScript.

Quick solution:

const numbers: number[] = [2, 3, 1];
const max: number = Math.max(...numbers);

console.log(max);  // 3

 

Reusable logic

const findMax = (array: number[]): number => {
    if (array.length === 0) {
        return NaN;
    }
    let result = array[0];
    for (let i = 1; i < array.length; ++i) {
        const entry = array[i];
        if (entry > result) {
            result = entry;
        }
    }
    return result;
};


// Usage example:

const max = findMax([2, 3, 1]);

console.log(max);  // 3

Output:

3

 

Alternative titles

  1. TypeScript - how to find maximum value in array
  2. TypeScript - get max value from array
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join