0

Trying to figure out what the ? and : mean in this one line return statement.

return input && input.length ? [input.filter(p => p > 0).length, input.filter(n => n < 0).reduce((a, b) => a + b, 0)] : [];
Jeff
  • 3
  • 1

3 Answers3

1

This is a ternary if statement, I'd use parenthesis for organization sake, but it works like this:

[condition] ? [returns if condition is true] : [returns if condition is false]

0

Return a ? a : b;

Means if a return a else return b!

Sphinx117
  • 97
  • 1
  • 5
0

It means that if the condition before ? is true then use the first part before : else use the second part.

In your case it means that if input is not null and has value, then return the filtered one otherwise return an empty array.

Yaser
  • 5,285
  • 1
  • 13
  • 26
  • In the OP it will return an array of `[a, b]` where *a* is the number of elements greater than zero and *b* is their sum, or `[]` (empty array). – RobG Jan 20 '17 at 03:41