Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
ste0910417yActually 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 -
C0D4681457y@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. -
C0D4681457yFarkin JavaScript 😆
This is how it’s equating.
=> [] == ![]
=> “” == false
=> 0 == 0
=> true -
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. -
Bikonja23867yThere'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.
Related Rants
JavaScript WTF #1
undefined
javascript
wtf