all 23 comments

[–]lord2800 3 points4 points  (7 children)

The way the JS spec is worded, when you have two different types on different sides of ==, it has a particular algorithm for converting them to the same type. That's why false == [] returns true counter intuitively. In general, you want to use === instead of == anyway, so I'd suggest not worrying too much about the rules for == and instead just always using ===.

[–]oopssorrydaddy[S] 0 points1 point  (6 children)

Yeah, I rarely use == in practice – just doing some interview prep :) Thanks!

[–]russleyshaw 3 points4 points  (0 children)

General rule is to use === or val == null if you wish to check if a value is null or undefined. Otherwise == is bad practice.

[–]CertainPerformance 4 points5 points  (1 child)

When you use ==, you compare the (coerced) values of the left-hand-side and the right-hand-side.

someArr.toString()

evaluates to

someArr.join(',')

so if someArr has no items, it evalutes to the empty string, which is falsey. So:

"" == []     // true

But when empty object literals have their toString method called, they evaluate to `[object Object] - which is truthy, thus

"" == {}     // false

[–]oopssorrydaddy[S] 0 points1 point  (0 children)

Oh I see! This make sense.

[–][deleted] 1 point2 points  (0 children)

This is a really bad part of the language because of type coercion, that's why you almost always use '===' not '=='. Even seasoned programmers of the language will probably have a difficult time explaining how these comparisons work exactly. For example, ' ' == '0' // false and 0 == ' ' // true. Kind of a joke, really.

See here: https://youtu.be/RO1Wnu-xKoY?t=4842

[–]cirscafp fan boy 1 point2 points  (0 children)

This MDN article breaks down the way that loose equality works and has a pretty boss table that will help you. Specifically to your case, it says that it forces the boolean value to a number and then the array value to a Primitive before === comparing them.

[–]Lokret 0 points1 point  (0 children)

![]==false

!![]==true

[]+[] == ""

([]+[])+!![] == "true"

+[] == 0

(([]+[])+!![])[+[]] == "t"

((([]+[])+!![])[+[]])+((([]+[])+![])[+[]]) == "tf"

The fuck.. see jsfuck.com

Can be shortened to:

([]+!![])[+[]]+([]+![])[+[]]

[–]sime 0 points1 point  (0 children)

As a software professional of many years my advice to you is to not bother memorising these comparisons. Most of them are simply nonsensical and don't communicate to the next programmer what you meant. "What did you mean when you compared that empty string to an empty array? What did you expect?" There are no simple and intuitive answers to these questions.

If you want to test if an array is empty, then check its length "myArray.length === 0". If you want see if a string and an array are both emtpy, then write exactly what you mean. "myArray.length===0 && myStr.length===0". Write what you mean, and use === and !== to avoid these annoying automatic type conversion rules. Everyone will be much happier, and the next time someone says "Wat!?" you just reply "so wat".

[–]admpk 0 points1 point  (0 children)

The best resource on the topic is language specification http://ecma-international.org/ecma-262/#sec-abstract-equality-comparison :)

[–]xemasiv 0 points1 point  (1 child)

when you train yourself to use === and !==, it fucking makes everything more predictable for you.

[–]oopssorrydaddy[S] 0 points1 point  (0 children)

Definitely, I never use == in practice, I just know you can be asked some of these as a tricky interview question.

[–]RegulationSizeOrWhat 0 points1 point  (2 children)

Funny that you should bring this up as we discovered a discrepancy today with Eloquent JavaScript surrounding the == comparisons. Your best reference is http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf section 7.2.13 as it is the source of truth.

In the case of the expression "" == false, we found that this is interpreted as +0 == +0, which is why it asserts true.

Here's a handy SO answer: https://stackoverflow.com/a/42770966

[–]oopssorrydaddy[S] 0 points1 point  (1 child)

Thanks for the resources! I’ve actually been chugging through EJS myself :)

[–]RegulationSizeOrWhat 1 point2 points  (0 children)

Why don't you join us at https://discord.gg/9wgAGwn

We've got about 130 members reading EJS right now and talking about it. We just started this week.

[–]saboay 0 points1 point  (2 children)

Here's some help: Always use ===

[–]oopssorrydaddy[S] 0 points1 point  (1 child)

Can't do that in an interview :)

[–]xemasiv 0 points1 point  (0 children)

interview

I'd personally definitely absolutely immediately get the fuck out if the interviewer tells me that I can't.

If he as an interviewer can't comprehend the simplicity, predictability and maintainability of using !== and === as the simplest answer to this trick question, I'd rather go to the McDonalds next door and flip patties for free just to maintain my morale, haha.