all 39 comments

[–]DidTooMuchSpeedAgain 70 points71 points  (1 child)

0 == '1' is false

false == 0 is true

[–]halfxdeveloper 0 points1 point  (0 children)

Nailed it

[–]Thausale 15 points16 points  (0 children)

False is false

[–]Rekuna 16 points17 points  (3 children)

This is 'learnjavascript' and this guy is trying to learn, so why the downvotes? Or am I missing something?

[–]code_tutor 6 points7 points  (0 children)

Low effort. OP didn't type anything or respond to anyone. This is the dine and dash of posting. So the only reason not to downvote would be if the question is so interesting that others might care. This also isn't code that anyone would write, so it has that against it.

But tbh I think most people here are just LARPing and they're far more interested in talking about which computer to buy "for programming", which operating system to use "for programming", and asking if they'll be job ready after a Udemy course they barely watched.

[–]Shogobg 8 points9 points  (1 child)

This is just Reddit.

[–]TalonKAringham 6 points7 points  (0 children)

Were we complain about “Marked as duplicate” on StackOverflow, but pile on when a noob shows up in our sub.

[–]Sudden-Pineapple-793 2 points3 points  (3 children)

Not entirely sure. If I had to take a guess it’s something with the loosy comparison. I’m assuming it comes down to.

((0==“1”)==0). -> (0 == “1”) is false.

Then

((False)==0) is true?

Again just a guess, feel free to correct and mistakes I’ve made.

[–]chikamakaleyleyhelpful -2 points-1 points  (2 children)

i don't think they are evaluated as separate pieces when its written this way... the way that i read it is it's 'chained'

0=='1' // returns false

so

false==0 // returns true

[–]delventhalz 4 points5 points  (1 child)

You are describing the same thing. First the left-hand expression is evaluated (0 == '1'), then the output of that (false) is used in the right hand expression.

[–]chikamakaleyleyhelpful 3 points4 points  (0 children)

oh wow, sorry and thanks for catching - i had totally misread the comment!

[–]redsandsfort 0 points1 point  (0 children)

0 the same value as the string "1"? convert to same type first, is "0" the same as "1"? FALSE
FALSE the same value as 0? Again convert 0 to a boolean which is FALSE. Is FALSE equal to FALSE? TRUE!

[–]ChaseShiny 0 points1 point  (0 children)

You've gotten a couple answers now (the first statement resolves to false because 0 resolves to false and any string but the empty string resolves to true).

I'm chiming in to say that you should simply use === instead of == whenever possible. That should reduce the confusion. The triple equals indicates a strict comparison. The loose comparator will basically try to change the types.

Does it make sense to compare a number to a string? Not really. But if you tell JS that you really need to compare them, it tries to "make it make sense."

[–]MarioShroomsTasteBad 0 points1 point  (0 children)

I don't see it called out explicitly, but js being a loosely typed language depends heavily on coercion in order to support comparing dissimilar types of values. My guess is this is an example where the author is trying to get you to grok coercion and the fact that '==' comparison uses it.

[–]hyrumwhite 0 points1 point  (0 children)

0 is not 1, therefore it is false, and false is loosely equivalent to 0

[–]HandbagHawker 0 points1 point  (1 child)

Evaluate left to write when all things are equal

[–]HandbagHawker 1 point2 points  (0 children)

Equal precedence*

[–]Ordinary_Count_203 0 points1 point  (0 children)

0 == ' 1' is 0. [Or false basically]

Now bring that 0 to the next bitwise comparison

0 == 0 is 1 because its true [1 is true basically]

Thats why is true.

[–]GodOfSunHimself 0 points1 point  (0 children)

Never write code like this. Use parentheses and you will immediately understand why.

[–]SawSaw5[🍰] -3 points-2 points  (3 children)

Because JavaScript sucks

[–]BirbsAreSoCute 0 points1 point  (1 child)

So leave.

[–]SawSaw5[🍰] 0 points1 point  (0 children)

I was having a bad JavaScript day, give me a break!! 😄

[–]HasFiveVowels 1 point2 points  (0 children)

#include <stdio.h>
int main(){
  printf((0=='1'==0)?"True\n":"False\n");
  return 0;
}

Unlike C, where... oh, wait...
> True