Click here to Skip to main content
15,917,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This might be a stupid question, but how do I compare multiple variables at once?

Say I have 3 varaibles: a, b and c, and I need to check whether they are all the same. Of course, a==b && b==c works, but is there a shorter syntax like a==b==c (this doesn't work)?
Posted
Updated 29-Aug-10 6:46am
v3

You could create a function that takes a list of variables and compares them all to another item (you'd use a for loop). You would use it something like this:
JavaScript
var matchesAll = EqualsAll(b, [a, c, d, e, f, g]);

That only cuts down on how much you have to type if you have a lot of variables though.
 
Share this answer
 
Comments
pimb2 30-Aug-10 1:35am    
This quite appeals to me, thanks!
No shorter syntax, that is all you get.
 
Share this answer
 
Comments
Dalek Dave 29-Aug-10 20:21pm    
I think there is.
try...

if (a-a)=(b-c)
 
Share this answer
 
Comments
[no name] 29-Aug-10 21:48pm    
Sorry I don't see how this is any shorter syntatically. Although the OP asked in reference to JavaScript if you compare the two methods in C# yours has one less instruction.
AspDotNetDev 30-Aug-10 0:27am    
DD, (a-a) will always be 0. That is, if a, b, and c are numbers at all (the OP didn't indicate that).
pimb2 30-Aug-10 1:35am    
Actually, it are objects that I'm comparing so that's not really a solution I'm afriad...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900