13
hack
22d

Its true unless its javascript, then its false. What the fuck is this? Someone got explanation?

Comments
  • 15
    reminds me of this image

    source: https://reddit.com/r/...
  • 3
    Can you explain? I went through each line and they all seem good to me according to the spec.
  • 1
  • 1
    Wait... the Image is correct

    What’s going on!????
  • 4
    That's how Mafia works.
    Nobody would actually use such comparisons in an actual program. If you need to check an arbitrary input for validity, you'd most likely check the type first

    typeof myVariable === "number"

    and then for a valid value

    0 <= myVariable

    Number.isNaN(...) is not a general function to abuse for abitrary inputs. NaN means it's not a valid number in the range of numbers. E.g. some bit pattern which is not a valid double-precision floating-point in IEEE 754.
  • 1
    @PonySlaystation

    But:

    NaN == NaN and NaN === NaN both return false
  • 4
    NaN is a special kind of a number it's like how null is an object.

    isNaN(arg) will try to convert the arg to a Number via Number(arg), and then check if it's a NaN via it's special comparison.

    ?: Number.isNaN, on the other hand, only checks if the argument is NaN, without correcting the type. Which will make it return false unless the arg is NaN.

    parseInt(arg) will try to return as many digits as it can until it reaches something not digit.

    Try parseInt on "d123" and "123" and see the difference. It's like regexp /^\d+/ . [1]
  • 2
    @Demolishun JS is compliant to IEE 754 which allow distinct NaNs. Some maffs guy can probably clarify further how they work.
  • 4
    Since NaN is an exceptional value, you can't just compare it with another thing.

    That's why isNaN function(s) exist.

    a NaN compared to a number (which includes the NaN value) will return false no matter what.

    But to be honest, I have never noticed that there was parseInt and parseFloat functions on the global, I always went to the Number object's methods.[2][END]

    [2]
  • 2
    NaN is more like an exception object than a value. No two NaNs are the same, and they cannot be compared. It's confusing at first, but it has a straightforward and easy-to-google answer
  • 0
    @melezorus34 So maybe the comparison needs to check the type of NaN against type of returned value?
  • 2
    Extra:

    ?:How does one check if an argument is NaN without isNaN functions?

    Easy, first of all, convert the argument to a number via Number() and save it somewhere.

    Then, check if the number is not equal to 0 (I use type strict eq as a habit)

    If it's not, return ! convertedArgument, NaN is a falsy value and ! operator will make it true.

    If it's equal, return false as usual.

    A shorter function can be written like this: @highlight

    argument=>Number(argument)!=0 && !Number(argument);
  • 1
  • 1
    @Demolishun nope, it's a bit more complex. I just explained here without realizing you sent something btw.

    As I said before, typeof NaN will give "number" too, so normal comparisons wouldn't cut.
  • 1
    @melezorus34 I believe you are using the same parse functions, I cannot find any in the spec for Number so it might be resolving them in the prototype chain.
  • 0
    ¯\_(ツ)_/¯ at least node is saying both functions are identical (===) and hasOwnProperty is giving true on both sides (Number, globalThis)
  • 1
    @Demolishun NaN is not comparable to NaN and it wouldn't make much sense anyway.
    Let's say you want to compare two function parameters, a and b, which you expect to be numbers:

    function someNums(a, b) {
    if ( a === b ) { return 0; }
    return a + b;
    }

    Since you expect both to be numbers, you should actually make sure of it, which is good practice:

    function someNums(a, b) {
    if (
    typeof a !== 'number' || Number.isNaN(a)
    && typeof b !== 'number' || Number.isNaN(b)
    ) {
    throw new Error( 'computer says "no"' );
    }
    if ( a === b ) { return 0; }
    return a + b;
    }

    You don't need to do it everywhere but it helps a lot to find bugs quicker and the almighty V8 will optimize it anyway.
  • 0
    I just read that you can check for NaN by comparing a variable to itself and it always returns false:

    (function (){var x=NaN; return x===x;})()

    If x is set to anything else it will return true.

    This is a really interesting topic!
  • 1
    @melezorus34 Looks like you were right, but according to MDN, Number's parse functions are not IE compatible.
  • 3
    @theuser I thought we were over IE.

    You need to enable ActiveX to see this content.
  • 5
    There are many paths to creating NaN, such as imaginary numbers like 3+2i: `3 + (-4)**(1/2)`, undefined real-math values like `Infinity * 0`, or undefined real-javascript values like `Number(undefined)`

    because of this, the hypothetical "value" of each NaN would differ (which we do not and cannot store), so all comparisons therefore must fail. All NaNs are created unequal.

    It's easy to demonstrate: `NaN + 2` returns `NaN` -- and these are obviously different values!
  • 3
    @Root But Infinity === Infinity is true. There are definitely different kinds of Infinity (positive and negative are 2).
  • 2
    @Demolishun but those are set values, Infinity is Number.POSITOVE_INFINITY
  • 5
    @Demolishun In js, `-Infinity == Infinity` is false, and `Infinity == Infinity` is true. This follows real math pretty accurately.

    Infinity in math is a concept, not a value, so it cannot have a magnitude. (2*infinity) is infinity. therefore all infinities (in the same direction!) are equivalent.

    The real mind-bending aspects come in when raising it to a power, performing operations on/between two infinities (or better still: infinite series!). As JS doesn't know how to approach these, it resorts to NaN instead of calling you out on your mathematical transgressions. Afterall, if you wanted to do true rigorous math, you'd use something like R or Haskell instead 😅
  • 1
    When comparing a value to NaN I expected it works as comparing undefined values. I see now why there is isNaN function. But something is not clear for me. Why isNaN('') returns false?
  • 3
    @hack Because '' is not a NaN object.
  • 1
    @hack I assume you meant "". If you provide a non Number argument as the parameter, it is coerced to a Number. So a more explicit alternative to isNaN("") is isNaN(Number("")).

    And what does Number("") return? It returns 0.
  • 1
    @theuser or... just Number.isNaN?
  • 1
    @melezorus34 Well hey, by using isNan, you don't need a tiny tiny polyfill for IE.
  • 1
    @Demolishun well, "yolo" is not a NaN object too but isNaN("yolo") returns true.

    @theuser I see the problem lies under Number(""). Thanks.
  • 2
    @hack Still not sure what the problem is though. Even without coercing to 0, an empty string still isn't a NaN.
  • 1
    @hack I dunno, jesus script is just magic.
Add Comment