DEV Community

Discussion on: Keeping Your Code Simple

Collapse
 
aminnairi profile image
Amin • Edited

I would probably go for a combo prototype/function.

// Can be used as-is
function longestString(first, second) {
  return second.length > first.length ? second : first
}

// But the prototype makes it more natural to read later
Array.prototype.longest = function() {
  return this.reduce(longestString, '')
};

// You know the winner is and will forever be..
const languages = [ 'javascript', 'php', 'ruby', 'python' ]

languages.longest() // "javascript" 😎