80
Comments
  • 11
    And people hate PHP 😂
  • 19
    Why would you ever do this
  • 9
    Maybe because using not on an empty array is fuckin weird
  • 13
    even Js compiler think you are weird so it treats you like that
  • 3
    @featurenotbug I don't know but it's fucking hilarious 😂😂
  • 3
    Makes no sense
  • 14
    P=NP solved by JS.
  • 3
    https://destroyallsoftware.com/talk... is amazing for js (and ruby) weirdness! 😀
  • 8
    Actually that totally makes sense ... According to the ECMAScript documentation:
    "!expr Returns false if it's single Operand can be converted to true; otherwise, returns true"
    WTF does that mean?
    In JavaScript an array [] is an object
    typeof([]) // 'object'
    and an object is treated as a "truthy", meaning that when its value is coerced to Boolean, evaluates to true.
    When you write
    ![]
    The result is false because [] can convert to true
    [] == ![] // true
    true == !(false) // true
  • 2
    @ste09
    That doesn’t make sense or I’m missing something here.

    [] is true
    ![] is true?....

    If ! Is NOT and [] is true than that becomes false ( Not true )

    So

    True == not true , is still false.
  • 6
    To array or not to array!! That is the question.
  • 4
    @ste09 I think I need more sleep 😂
  • 10
    Farkin JavaScript 😆
    This is how it’s equating.

    => [] == ![]
    => “” == false
    => 0 == 0
    => true
  • 5
    It is a reference to an object in memory.

    [ ] == [ ] // false.

    Because they refer to different memory addresses. Let's call those references 0x243 and 0x867. Those numbers are not equal to each other.

    ! [ ] is "not a reference". Let's call this 0x000, null, or super falsy. Whatever. That does not equal 0x243 or 0x867 either, right?

    False! (haha pun)

    The problem is that ==, unlike ===, does things with the expressions on both sides. It converts booleans to numbers. It converts objects to primitives using valueOf and toString.

    ! [ ] is 0 in the eyes of ==.
    [ ] is an empty string in the eyes of ==.

    Those are equal(ish).

    Now, stop being a retard and use strict comparisons. Unless you know what you're doing, but then you wouldn't have been stumped by this idiosyncratic behavior.
  • 2
    There's so many reasons this could be correct and the only reason why someone would think it's not correct is because the thing on the left looks equal to the thing that's being !ed on the right. Which also crumbles once you look at what that thing actually is (an array, which is a ref type, not a value type).
    So whoever thinks this is weird is definitely a bad programmer.
  • 1
    @Bikonja
    // []==![1]
    // true
  • 0
    Justifiable, [ ] is a falsy value, so ![ ] is a Truthy value, no wait, WTF
Add Comment