JavaScript > / < / === three way comparison without having to write the compared values more than once?

I'm trying to write a solution to a problem that requires three possible different return values depending on the result of two values. The possibilities are greater than, less than, or equal to. An easy and understandable way to write this would be:

function compare(x, y) {
  if (x < y) return y;
  if (x > y) return x;
  if (x === y) return 'equal';
}

I've been trying to figure out if there are ways to write this by reducing the number of occurrences of x, y, if, and return in the code. For instance, if I'm not checking for ===, then I can eliminate if and reduce return to one use:

function compare(x, y) {
  return x > y ? x : y;
}

Adding === back into this ternary return is clunkier, and I'm not a fan of using ternary operators when there are more than two possibilities. This could be really difficult to read if x and y were replaced by long expressions:

function compare(x, y) {
  return x === y ? 'equal' : x > y ? x : y;
}

I could also do:

function compare(x, y) {
  if (x !== y) {
    return x > y ? x : y;
  }
  return 'equal';
}

which could be more readable than the function above, especially if x and y are replaced with expressions.

Using a switch in this case could be overkill -- but maybe not? Is there another solution to this? Is there a way to store the result of comparing x and y and then checking what that value is? Is it even worth the trouble at that point?

Answer:1

I want to build my website using Javascript for front end and PHP for backend. I want to know how can I keep header & footer same on all pages if I am using javascript for client side. I can do it ...

I want to build my website using Javascript for front end and PHP for backend. I want to know how can I keep header & footer same on all pages if I am using javascript for client side. I can do it ...

  1. freeze header footer
  2. keep header and footer on first page only
  3. keep header and footer on all pages in word
  4. keep header remove footer
  5. excel remove header keep footer
  6. word remove header keep footer

response is JSON Edge: sometimes inserts response property value into DOM briefly then removes it, sometimes logs error "SCRIPT5: access denied" (indicating CORS), response fully accessible from ...

response is JSON Edge: sometimes inserts response property value into DOM briefly then removes it, sometimes logs error "SCRIPT5: access denied" (indicating CORS), response fully accessible from ...

  1. xmlhttprequest access response

I am making a digital clock. I made a button and this button changes clock's format(between AM/PM System and 24 hours system). But the problem is when i switch Intervals, both intervals working at the ...

I am making a digital clock. I made a button and this button changes clock's format(between AM/PM System and 24 hours system). But the problem is when i switch Intervals, both intervals working at the ...

Was trying to do an restful update. my model.js module.exports.updateGenre = function (id, genre, callback) { var query = {_id: id}; var update = { name: genre.name } Genre....

Was trying to do an restful update. my model.js module.exports.updateGenre = function (id, genre, callback) { var query = {_id: id}; var update = { name: genre.name } Genre....