This question already has answers here:
Double structural equality operators: if(a==b==c)
(2 answers)
Closed 8 years ago.
I've got 3 random numbers (in this specific case between 1 and 7 but it doesn't really matter).
I want to check whether I got "three of a kind" by using
if (x==y==z) {
code
}
The problem is that when x==y and z==1 x==y==z will return true. How do I check whether x, y and z actually got the SAME value?
Example: 5==5==1 will return true, how do I check for 5==5==5 specifically? (Excluding 5==5==1)
By doing a proper comparison:
x === y && y === z
// due to transitivity, if the above expression is true, x === z must be true as well
x==y==z is actually evaluated as
(x == y) == z
i.e. you are either comparing true == z or false == z which I think is not what you want. In addition, it does type conversion. To give you an extreme example:
[1,2,4] == 42 == "\n" // true
The problem is that when x==y and z==1, x==y==z will return true.
Yes, because x == y will be true, so you compare true == 1. true will be converted to the number 1 and 1 == 1 is true.
You should check with separate && operations
if(x == y && x == z){
//all are equal
}
Related
This question already has answers here:
Javascript if (x==y==z): [duplicate]
(2 answers)
Closed 2 years ago.
I'm trying to write a flexible query logic based on a config payload, e.g:
{
"test": "( fieldA == fieldB == fieldC )"
}
When my app replaces 'fieldA', 'fieldB' and 'fieldC' value with text data, the result is always false.
This clearly has to do with me confusing the way javascript runs the comparison (it must have something to do with data types and math ordering), but can anyone explain this with a simple example for alphanumeric values?
e.g. 'John Smith' == 'John Smith' == 'John Smith' is always false;
x == y == z does not do what you think :
it translates to (x == y) == z.
With the quirks of javascript's == operator :
1 == 1 and 'A' == 'A' both translate to true
however : true == 1 returns true, while true == 'A' doesn't
You probably want to rewrite your condition to :
(x == y) && (x == z)
and very probably want to use the unambiguous === operator :
(x === y) && (x === z)
to avoid pitfalls such as the one you falled upon (true == 1 checks, but not true === 1)
A==B==C or A==A==A is legally allowed.
But the problem is, A==C never happens or in your case fieldA==fieldC never happens. Hence this leads to ambiguity
This question already has answers here:
Javascript: the confuse about comparison "2 == true"
(2 answers)
Closed 5 years ago.
if var a = true then a == 1 is true but a == 2 is false. Why? I understand that in javascript boolean expressions 0 casts to false and 1 casts to true. But what about the other integers. Why the above behavior?
Why? I understand that in javascript boolean expressions 0 casts to
false and 1 casts to true.
Because Number(true) => 1
As per spec of abstract equality expression evaluation
If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
Hence a is first coerced into a Number and then compared with 1.
when you use == or != in js,the operands will be converted to the same type before making the comparison.when converting:
if there is a Boolean, true will be turned to 1 and false turned to 0;
var a = true;
a == 1;// a turned to 1 before compare,so true
a == 2;// a turned to 1 before compare,so false
This is to do with coercion. In the a == 1 scenario, a is coerced into a an integer to compare again 1, true is coerced to 1. In the second example, a is again coerced to 1, so it doesn't equal 2.
Further reading on coercion
You may convert 1 to True and True to 1 but you can't convert other numerics to True.
That's probably inherited from binary world and it makes sense, because in binary 0 is false and 1 is true. Exactly, that is how type conversion been done for boolean types in javascript as well while comparing.
For boolean there are only 2 possibles, true or false. Hence in number/binary system 0 and 1 are apt to represent them.
Well because 0 = false and 1 = true. There are only two values for a boolean: true/false. So what should 2 be?
var has no type, it can be everything. Please consider also the == vs === operator.
var x = 1;
console.log(x === true);
console.log(x == true);
x = 2;
console.log(x === true);
console.log(x == true);
x = 0;
console.log(x === false);
console.log(x == false);
x = -1;
See the example: http://jsfiddle.net/2aqGS/146/
What I'm trying to is run some code if the number of input[type="checkbox"] is checked equal to the value of 2 or 3.
Here is an example of my JQuery:
if ($('input:checkbox:checked').length == 2 or 3) {
// run some code
}
Im not sure how to program it to understand whether the value is 2 or 3.
Any ideas?
Use the or operator ||
if ($('#id').length === 2 || $('#id').length === 3) {
// run some code
}
Please read up on Javascript logical operators. This is basic stuff.
Logical operators are used to determine the logic between variables or values.
Given that x = 6 and y = 3, the table below explains the logical operators:
Operator Description Example
========================================================
&& and (x < 10 && y > 1) is true
|| or (x === 5 || y === 5) is false
! not !(x === y) is true
While writing simple snippet for comparison in JavaScript I observed some weird behavior.
Case 1:
typeof(window.WHTStatement.DDL_TPTypeID.size()) ==> "number"
typeof(window.WHTStatement.Txt_TPTypeValue.size()) ==> "number"
window.WHTStatement.DDL_TPTypeID.size() == 1 == window.WHTStatement.Txt_TPTypeValue.size()
returns true -- OK
Case 2:
window.WHTStatement.DDL_TPTypeID.size() === 1 == window.WHTStatement.Txt_TPTypeValue.size()
returns true -- OK
Case 3:
window.WHTStatement.DDL_TPTypeID.size() === 1 === window.WHTStatement.Txt_TPTypeValue.size()
returns false, why?
What exactly happening here in case 3. Can somebody elaborate?
Unlike Python, in JS x == y == z is not equal to x == y && y == z but (x == y) == z. So you are actually comparing a boolean to a number which obviously fails in a type check.
The == comparison worked because 1 == true is true.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
When would JavaScript == make more sense than ===?
What is the difference between below methods in comparing a string with undefined value.
var x;
if(x==undefined)
{
alert(x);
}
and
if(x===undefined)
{
alert(x);
}
Why should i prefer second method in this case.. Please let me know advantages..
== attempts to convert the values to the same type before testing if they're the same. "5" == 5
=== does not do this; it requires objects to be of the same type to be equal. "5" !== 5
In this case, the result is:
x == undefined will be true if x is undefined or null.
x === undefined will only be true if x is undefined.
You should prefer the first method if you'd like undefined and null to be treated equivalently. One common use of this is optional function arguments.
function greet(name, greeting) {
if (name == undefined) name = 'World';
if (greeting == undefined) greeting = 'Hello';
alert(greeting + ' ' + name);
}
greet(); // alerts "Hello World"
greet("Bob"); // alerts "Hello Bob"
greet(null, "Goodbye"); // alerts "Goodbye World"
suppose we have x=5,
== is equal to
x==8 is false
x==5 is true
=== is exactly equal to (value and type)
x===5 is true
x==="5" is false
Hope you understand this concept
=== checks for the same type as well. You'll understand with a few examples:
(1 == '1') //Returns true
Since == doesn't bother with types, that returns true. However, if you want strict type checking, you'd use === because that returns true only if the it's of the same type, and is the same value.
(1 === '1') //Returns false
(1 === 1) //Returns true
Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding
positions.
Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything,
including NaN. Positive and negative zeros are equal to one another.
Two Boolean operands are strictly equal if both are true or both are false.
Two objects are strictly equal if they refer to the same Object.
Null and Undefined types are == (but not ===).
Reference
== is just comparing the two values, and if they are of different types, type conversion is done
=== compares the values and well as their types - so no type conversion will be done here.