Clarification on a statement inside a function

I need some clarification on this statement “return a === b ? 0 : a < b ? 1 : -1;” inside the below function, please.

function reverseAlpha(arr) {
  return arr.sort(function(a, b) {
    return a === b ? 0 : a < b ? 1 : -1;
  });
}

That’s a nested ternary.

Ternaries are introduced here:

Multiple ternaries are introduced here:

1 Like

@mukhtar-github It looks like you have a test statement in your conditional (ternary) operator where there should not be.

1 Like

Thank you . I will go through the link you provided.

Thank you. I will go through the links you provided.

Nothing wrong with the OP’s nested ternary. It could be written as follows to make it a bit more readable (allows you to see the nesting easier).

function reverseAlpha(arr) {
  return arr.sort(function(a, b) {
    return a === b
      ? 0
      : a < b
        ? 1
        : -1;
  });
}
2 Likes

Thank you. I appreciate your effort Mr Randell Dawson.