How to find the longest string in an array of strings in TypeScript

1 Answer

0 votes
const strings: string[] = ["c++", "python", "c#", "typescript", "swift", "java"];
 
const longestString: string = strings.reduce((a, b) => a.length >= b.length ? a : b);
 
console.log(longestString);

 
 
/*
run:
 
"typescript" 

*/
 

 



answered Oct 2, 2024 by avibootz
...