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

Viewed 129

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;
}
2 Answers

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.

Here's the >= comparison table created with editing this JSFiddle.
You can see value >= value is true except when value = undefined or value = NaN:

enter image description here

Related