0

Today i solved a Task on CodeFights in JavaScript and looked afterwards for a better solution. The nr.1 JS solution was this :

function allLongestStrings(inputArray) {
    "use strict";
    let maxValue = Math.max(...inputArray.map(x => x.length));
    return inputArray.filter(x => x.length === maxValue);
}

I am quite new to Coding, but i learned some neat things from this already, like the "use strict", the map and the filter function. But my issue so far is that i don´t 100% understand how (x => x.length) exactly works. Is it declaring a new variable called x and applies x.length to it? Or what exactly is happening here?

georg
  • 195,833
  • 46
  • 263
  • 351
  • 1
    It's an [arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), which takes `x` and returns its length. – georg May 09 '17 at 16:45
  • 1
    The length will be 0 when the array is empty. 0 is falsey so the filter filters empty arrays. – Andrew Li May 09 '17 at 16:45

0 Answers0