Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 797
catwith Avatar asked Jun 22 '26 09:06

catwith


1 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.

like image 89
mbojko Avatar answered Jun 24 '26 21:06

mbojko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!