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

Time:04-12

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

CodePudding user response:

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.

CodePudding user response:

the operator >= means more or equal to

so if the value would be 100 it would select 100 and all values above

  • Related