why when you launch this code " console.log(1+1==true); " the output say " false "
and i want a solution for to become true with only changing one value
Please include your formatted code and a link to the lesson.
See:
The result of 1+1, 2, is not equal to true. A number is not equal to a boolean (like true or false). So the expression inside your console.log statement, 1+1 == true, is asking whether 2 is equal to true, which it is not. Thus, the statement logs “false” to the console. I don’t understand the context of why you want this code to respond differently, so I cannot provide anything more in terms of “fixing” it.
If you changed the value of 1 to 0 like this :console.log(1+0==true);
The output will respond true, because the 1+0 is 1, and if the output of the Operation differs in its output from 1, the answer will always be false, so my question was why?
Will the answer is simple true in computer language translate to 1 and false to 0, when you said 1+1==true,is like saying 2==1 and it’s false, but if tge operation equal to 1==1 that’s true, it’s is just a trick question to know why does the code act in such different way and sometimes Unexpected
This has to do with the weirdness of the specific comparison operator you’re using, ==. This is called the loose equality operator and it has a bunch of quirks. Basically, you should almost always use the triple === strict equality operator.
In this case, it has to do with the binary nature of machine language and the way that was interpreted when designing JavaScript. Essentially, when using ==, the types of both parts are converted and “massaged” to see if there’s a reasonable way you could say that they’re equal. True gets converted to 1, and then it asks, is 1 equal to 1? Of course it is, so 1 == true is true, but 2 == true is false because it’s the same as saying 2 == 1.
Well thanks for the information i just started JavaScript so I’m still beginner , i used == as if i were in python ,and after reading about it i noticed that there is a different
==
is used for loose (type-coerced) equality comparison.===
is used for strict (value and type) equality comparison.
Ah, well welcome from Python! JavaScript is very quirky, so buckle in!