This question already has answers here:
Why does "true" == true show false in JavaScript?
(5 answers)
Closed 2 years ago.
I'm trying to understand the following case:
> "1" === 1
false
> "1" == 1
true
> true === 1
false
> true == 1
true
> "true" === true
false
> "true" == true
false
Why javascript comparison ("true" == true) is false?
Also, what is the proper way to compare this one?
Question has been asked before here.
In essence, "true" is converted to NaN, while true is converted to 1 (which is a boolean. Hence, they differ.
The Boolean in the comparison is going to have an implicit conversion to Number (0/1) so 1=true gets true, but "true" == true (1), so the string can't be equal to Number, it gets false
Related
This question already has answers here:
Can I use chained comparison operator syntax? [duplicate]
(6 answers)
Why does (0 < 5 < 3) return true?
(14 answers)
Closed 1 year ago.
I have a question about how chain equality works in JavaScript.
For example in python if you have:
a, b = 1, 2
a == b == False //False
Because it converts to:
(a == b) and (b == False)
So, finally it is False.
But when I try this in js:
console.log(1==2==false) // true
I got "true". I don't know why and how it is worked in js.
could you please help me out?
Reading left to right:
1==2 is false
false==false is true
in the code 1 == 2 == false
we read it as 1 == 2 == false
so basically 1 == 2 is false
and fasle == false is true
This question already has answers here:
What does "!" operator mean in javascript when it is used with a non-boolean variable?
(5 answers)
Closed 2 years ago.
I looked a lot, but I couldn't find an answer for this especific case.
Why does this expression return true?
let variable = 0
!variable // true
I understand that the ! mark checks if a value is null or undefined, but in this case variable is defined. This is tricking me.
Isn't 0 really considered a valid value?
! is known as the logical NOT operator. It reverses the boolean result of the operand (or condition)
0 is also considered as the boolean false, so when you use !variable you are using the logical operator and saying it to change the value of the variable to its opposite, that in boolean is true
0 == false == !1 == !true
1 == true == !0 == !false
in Javascript are considered false:
false, null, undefined, "", 0, NaN
are considered true:
true, 1, -0, "false". <- the last one is a not empty string, so its true
if( false || null || undefined || "" || 0 || NaN) //never enter
if( true && 1 && -1 && "false") //enter
https://developer.mozilla.org/en-US/docs/Glossary/Falsy
To quote MDN Web docs, the Logical NOT !:
Returns false if its single operand can be converted to true; otherwise, returns true
So in your case it returns true because 0 can be converted to false
You can check out this link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
console.log(false === 0) // false
console.log(false === !1) // true, why does it equate to true using !?
and vice versa for
console.log(true === 1 ) // false
console.log(true === !0) // true
I understand the difference between equality and identity but couldn't understand this behaviour of JS . Please explain ?
The === requires that both values are the same type, no implicit coercion is performed.
When you compare a boolean to a number with triple ===, you will always get false, since they are not the same type.
But using ! in front of a number (or anything else) will convert it to Boolean first, (!x is same as !Boolean(x)) so the strict comparison can succeed. (actually, using ! on anything will coerce it to a Boolean in JS)
Rules of conversion number-to-boolean conversion :
For the number-to-boolean conversion, 0 and NaN are coerced to false, and any non-zero number is coerced to true. (FYI, null, undefined and the empty string '' are the only other falsy values in JS)
Now, you will have two boolean to compare, and === will return true or false accordingly.
So, to summarize :
in !1, 1 is non-zero, so it gets coerced to true, and !true gives false
so false === !1 is equivalent to false === false, which is a true statement.
You can work out the details for the other comparison.
As an additional resource, if you have time and are interested in learning more, I recommend the very good free ebook "You don't know JS".
Because !1 is treated as a boolean value in JS whereas 0 or 1 are interpreted as number.
You can use JS typeof !1 and typeof 0 to see the difference.
Since, false is also a boolean type, matching it with 0 (number) will result false.
Hope that clears it up.
When you do false === 0, it's comparing the boolean false to the integer 0. In contrast, when you do false === !1, javascript converts the integer 1 to a boolean and the unary ! acts as a "not" operation on what is effectively true. Since 1 is truthy in javascript, !1 converts to false. And vice-versa.
console.log(0) // 0
console.log(!0) // true
console.log(1) // 1
console.log(!1) // false
!1 is a Boolean (because of the logical NOT operator (!)).
true and false are also Boolean values.
0 is a Number.
Values of different types are never identical (===).
JavaScript will first convert your inverted numer to a boolean:
console.log(false === 0)
-> false
console.log(false === !1)
-> false === false
-> true
and vice versa for
console.log(true === 1 )
-> false
console.log(true === !0)
-> true === true
-> true
Do you understand the difference between an integer and a boolean?
console.log(false === 0) // false
console.log(false === !1) // true
console.log(true === 1 ) // false
console.log(true === !0) // true
console.log(!1) // false
console.log(!0) // true
Identity comparison checks that an object is exactly that, and 1 is not true, neither is 0 false.
Because ! operator convert any value to Boolean type and you are checking against another Boolean that's why false === !1 is giving you true.
This question already has answers here:
How does (A == B == C) comparison work in JavaScript?
(6 answers)
Closed 8 years ago.
function a() { return (1 == 1 == 1); }
function b() { return ("1" == "1" == "1"); }
function c() { return ("a" == "a" == "a"); }
I tested the above code in Chrome's console and for some reason, a() returns true, b() returns true, and c() returns false.
Why is this so?
Because you are comparing the (boolean) result of the first equality with the (non-boolean) third value.
In code, 1 == 1 == 1 is equivalent to (1 == 1) == 1 is equivalent to true == 1.
This means the three methods can be written more simply as:
function a() { return (true == 1); }
function b() { return (true == "1"); }
function c() { return (true == "a"); }
These comparisons work according to these rules (emphasis mine):
If the two operands are not of the same type, JavaScript converts the
operands, then applies strict comparison. If either operand is a
number or a boolean, the operands are converted to numbers if
possible; else if either operand is a string, the string operand is
converted to a number if possible. If both operands are objects, then
JavaScript compares internal references which are equal when operands
refer to the same object in memory.
So what happens in c is that "a" is converted to a number (giving NaN) and the result is strictly compared to true converted to a number (giving 1).
Since 1 === NaN is false, the third function returns false. It's very easy to see why the first two functions will return true.
Because 1 == true
But "a" != true
So basically what happens is that
1 == 1, "1" == "1" and "a" == "a" are all evaluated to be true and then compared to the next value.
The string "1" is converted to a number (1) prior to being compared to true and is thus also considered to be equal to true.
Now, the "WHY?!?!" question is explained by the fact that Javascript has its roots in the C-family of languages. In which any number, other than 0 is considered to be a valid true boolean. :-/
Because 1 and "1" are both converted to true, as numbers. This is not the case with "a". Therefore:
("1" == "1" == "1")
evaluates to
(("1" == "1") == "1")
which evaluates to
(true == "1")
Similarly,
("1" == 1 == "1")
is also true, or any combination thereof. In fact, any non-zero number when converted to a boolean is true.
Whereas, "a" does not evaluate to true.
It's because JavaScript is a weakly typed language. This means that it is not expressive enough to talk about types, and in fact implicitly coerces values to belong in types to which they have no semantic relation. So, (1 == 1) == 1 evaluates to true because (1 == 1) correctly evaluates to true, so that JavaScript evaluates (true) = 1. In particular, it is turning 1 to a boolean (or true to a number -- I forget which, but the result is effectively the same).
The point is that JavaScript is turning one type of value into another type of value behind your back.
Your question shows why this is a problem: ('a' == 'a') == 'a' is false, because ('a' == 'a') is true, and JavaScript ends up comparing (true) == 'a'. Since there is just no sensible way to turn a Boolean into a letter (or a letter into a boolean), that statement is false. Of course, that breaks referential transparency for (==).
It's true that (1 == 1) == 1. Then it will be true == 1, but not in a == a == a.
Boolean handled as bits, each bit stands for true or false ( 1 for true, 0 for false )
so that 1 stands for true, and 0 stand for false
and 1 == 1 == 1 will be like (1 == 1) == 1, true == 1, true
while 'a' == 'a' == 'a' will be ('a' == 'a') == 'a', true == 'a', false
BONUS: 0 == 1 == 0, 1 == 0 == 0 and 0 == 0 == 1 returns true
This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Difference between == and === in JavaScript [duplicate]
(2 answers)
Closed 8 years ago.
I am learning Javascript with codecademy, and I was doing some comparisons, and for my code I did:
`console.log(1 == 2)`
and it returned False.
I also did:
`console.log(2*2 === 3)`
and that also returned False.
To check that I have not made a mistake, I did:
`console.log(1 == 1)`
and that returned True
The instructions tell me that === means equal to.
Are there any problems with using == instead of ===? And, which is better to use and why?
Thanks for any help you can give me!
Using == compares only the values, === compares the type of the variable also.
1 == 1 -> true
1 == "1" -> true
1 === 1 -> true
1 === "1" -> false, because 1 is an integer and "1" is a string.
You need === if you have to determine if a function returns 0 or false, as 0 == false is true but 0 === false is false.
It really depends on the situation. It's usually recommended to use === because in most cases that's the right choice.
== means Similar while
=== means Equal. Meaning it takes object type in consideration.
Example
'1' == 1 is true
1 == 1 is true
'1' === 1 is false
1 === 1 is true
When using == it doesn't matter if 1 is a Number or a String.
http://www.w3schools.com/js/js_comparisons.asp
== is equal to || x==8 equals false
=== is exactly equal to (value and type) || x==="5" false
meaning that 5==="5" false; and 5===5 true
After all, it depends on which type of comparison you want.