Double compare in JavaScript looks weird [duplicate] - javascript

This question already has answers here:
chaining double equals == returns false [duplicate]
(3 answers)
Closed 7 years ago.
Let's var a = "first", b = "second", c = "first";.
Expression a == c returns true, (of course!); a == c is true too.
Then why does a == a == c return false?
Example
var a = "first", b = "second", c = "first";
write( a != b != c );
write( a == b != c );
write( a == b == c );
write( a != b == c );
write("");
write( a == a == c );
// -------------------
function write(val) {document.querySelector("console").innerHTML += "<hr noshade size='1px' color='#eee'>" + String(val);}
<console></console>

Because what is really happening is this:
(a == a) == c
Which is really just:
true == "first"
See Operator Precedence.

Because it goes from left to right, one == at a time and the result of each previous == operation is a boolean:
(a == a) === true
(true == c) === false
You need to do:
a == a && a == c
or better yet (since this is JavaScript):
a === a && a === c
Alternatively, can write in CoffeeScript, which will translate:
a == a == c
into
(a === a && a === c);
for you.

a == c returns a boolean (true).
Since a is not a boolean, it cannot possibly equal a == c.

Related

JS syntax for "if a or b or c but not all of them" [duplicate]

This question already has answers here:
How to xor three variables in javascript?
(5 answers)
Closed 7 months ago.
So, I am super new to JS! I am trying to write a syntax to check if only one of the three variable is present.
if a and (not b or not c) or b and (not a or not c) or c and (not a or not b)
Try this out:
if ((a || b || c) && !(a && b && c)) {
}
Here's a test:
function specialBool(a, b, c, message){
if ((a || b || c) && !(a && b && c)) {
console.log(message)
}
}
specialBool(true, false, false, 'One true');
specialBool(true, true, false, 'Two true');
specialBool(true, true, true, 'All true'); // Shouldn't log
Convert them to booleans, then add them up, and check that the result is 1. (true will be coerced to 1; false will be coerced to 0.)
if (Number(Boolean(a)) + Number(Boolean(b)) + Number(Boolean(c)) === 1) {
// ...
}
or
const sum = [a, b, c].reduce((sum, item) => sum + Number(Boolean(item)), 0);
if (sum === 1) {
// ...
}

You Don't Know JS: Up & Going - example problem

In page 38 I found - arrays are by default coerced to strings by simply
joining all the values with commas (,) in between. You might think
that two arrays with the same contents would be == equal, but
they’re not:
var a = [1,2,3];
var b = [1,2,3];
var c = "1,2,3";
a == c; // true
b == c; // true
a == b; // false
But when I run my code like the following:
var a = [1,2,3];
var b = [1,2,3];
var c = "1,2,3";
console.log(typeof a == c); // false
console.log(typeof b == c); // false
console.log(typeof a == b); // false
I got different answers! Why typeof a == c and typeof b == c is false here?
its doing
typeof a == c
=> (typeof a) == c
=> "object" == c // which is false
basically typeof operator works before == sign
My cliche advice, always use paranthesis to make your intent clear
typeof (a==b) vs (typeof a) == b
The typeof operator returns the type of the content of a variable as a string. So if your variable a contains an array, typeof a will return the string "object" (as arrays are a special kind of objects).
So typeof a == c and typeof b == c compare the string "object" with the string "1,2,3". As they are obviously not the same, this returns false.
Note that the typeof operator has a higher precedence than the == operator.

Is >= equal to > || === or > || == in JavaScript? [duplicate]

This question already has answers here:
Why `null >= 0 && null <= 0` but not `null == 0`?
(6 answers)
Closed 4 years ago.
Is the below if statement
if (a >= b)
Equal to this?
if (a > b || a === b)
Or is it equal to this?
if (a > b || a == b)
It is equivalent to if(a > b || a == b)
var a = "2";
var b = 2;
console.log(a >= b); // true
console.log(a > b || a == b); // true (== compares value)
console.log(a > b || a === b); // false (=== compares value and type)
You can test it in the console:
var a = 0;
var b = '0';
a == b; // true
a === b; // false
a >= b; // true
Ergo, >= is equivalent to > || ==.
The actual result depend on the use case if the typeof both a & b is same then (a >= b) is same as (a > b || a === b). This is because == is equality with type coercion
var a = "2";
var b = "2";
console.log(a >= b); // true
console.log(a > b || a == b); // true
console.log(a > b || a === b); // true
var a = "4";
var b = 4;
console.log(a >= b); // true
console.log(a > b || a == b); // true
console.log(a > b || a === b); // false
Actually the first one
if(a >= b)
is similiar to
if(a > b || a == b)
but not equals to
if(a > b || a === b)
because in this last one you are even comparing the type of both the operands.
Example:
x = "5"
console.log(x==parseInt(x)) will return true
console.log(x===parseInt(x)) will return false
So, == does not consider the types of operands.
It depends. If before the statement you had defined a or b with a different type,
if(a > b || a === b)
will return false if the first clause is not true. However if you didn't define a or b before both will have the same type and both expressions are equivalent.
You can understand "===" as
(a == b && sameType(a,b))

jQuery if condition 1 or condition 2 is true than

I am trying to alert "yes" if ether of the conditions in my if statement are true:
var a = 2;
var b = 1;
if (a = 1 or b = 1 ) {
alert('yes');// should alert in this case
} else {
alert('no');
}
https://jsfiddle.net/90z7urvd/1/
What do I use for the if, if this is possible?
a = 1 will set the value 1 to variable a. It is not doing a comparison. For comparison, you use === or ==
=== (Identity operator) is the correct way to compare if both the types are same.
if (a === 1 || b === 1 ) {
=== operator won't do the type conversion before the comparison while == does the type conversion before the comparison.
For your or case, You may use || operator
var bootresul = someExpression || anotherExpression
Corrected code
var a = 2;
var b = 1;
if (a === 1 || b === 1 ) {
alert('yes');
} else {
alert('no');
}
You are assiging value rather then comparing
Try like this
if (a == 1 || b == 1)
To compare strictly use ===
Like this
if (a === 1 || b === 1)
JSFIDDLE
you can do this
var a = 2;
var b = 1;
if ((a == 1) || (b == 1 )) {
alert('yes');// should alert in this case
} else {
alert('no');
}
the == is one of the relational operator for checking equality and || is a logical operator that is a notion of logical OR
use this to compare just values
if (a == 1 || b == 1){
}
OR use this to compare values and type of variable
if (a === 1 || b === 1){
}
note : == will just check of values and === this will check value with type of variable
var a = 2;
var b = 1;
if (a == 1 || b == 1 ) {
alert('yes');// should alert in this case
} else {
alert('no');
}
I think you were doing assignment instead of comparison
Try using this:
if(a === 1 || b === 1){
alert('YES!')
}else{
alert('NO!')
}
OR you can use ternary operator condition instead of if else
(a == 1 || b == 1) ? alert('YES!') : alert('NO!')

How to know which condition was true in an if statement?

How can I know which condition in an if statement in JavaScript was true?
if(a === b || c === d){ console.log(correctValue) }
How can I know if it was either a === b or c === d?
Edit: I wanted to know if there was any way of doing this besides checking each condition on it's own if statement.
You can't.
If it matters, it needs to be two different conditions.
if (a == b) {
// it was a == b
return true;
}
if (c == d) {
// it was c == d
return true;
}
Note that even so, you won't know if both or just one of these conditions is true.
If you want to know this as well, you'll want an additional if:
if (a == b && c == d) {
// a == b and c == d
} else if (a == b) {
// just a == b
} else if (c == d) {
// just c == d
}
return (a == b || c == d);
If you really need to know which condition was true, just test them separately:
if(a == b) {
console.log("a == b");
return true;
} else if(c == d) {
console.log("c == d");
return true;
}
Alternatively, you might prefer something like this:
var result;
if ((a == b && result = "a == b") || (c == d && result = "c == d")) {
console.log(result);
return true;
}
This code is effectively equivalent to the former, however, I wouldn't recommend using this in a production system. It's much harder to read and guess the original intent of the code—it's likely that the person reading this after you would think the result = was supposed to be result ==. Also note that because empty strings are falsy, you must ensure that the string you assign to result is never empty, otherwise it would never enter the if-block.
You can try something like this:
if(e = (a === b) || (f= c === d)){ console.log(e, f, correctValue) }
Then You can check whether e or f is true, then make your changes.
I don't know about memory leak or something for this but yeah it works.

Categories

Resources