Search code examples
javascriptd3.jsconditional-statementsidiomscomparison-operators

What does `value >= value` do in JavaScript?


Below is one of conditional statements from the source code of d3.min.
What is this checking for?:

value >= value

Here is the entire source code:

export default function min(values, valueof) {
  let min;
  if (valueof === undefined) {
    for (const value of values) {
      if (value != null
          && (min > value || (min === undefined && value >= value))) {
        min = value;
      }
    }
  } else {
    let index = -1;
    for (let value of values) {
      if ((value = valueof(value, ++index, values)) != null
          && (min > value || (min === undefined && value >= value))) {
        min = value;
      }
    }
  }
  return min;
}

Solution

  • It could be a peculiar form of checking against NaN (EDIT: and undefined):

    const foo = 0/0; // NaN
    let bar;
    console.log(foo >= foo);
    console.log(bar >= bar);

    Although why would anyone write it like that, instead of using the isNaN method, I cannot tell. It's shorter than, say !isNaN(value) && value !== undefined, but I'm not sure the tradeoff in legibility is worth it.