3

I would like to compare the 3 javascript variable values and they are equal, need to perform a function.

if(vcount === tcount === lcount){

//do something;
}

and (as per this previous answer)

if((vcount == tcount) && (tcount == lcount)){

    //do something;
    }

But its not working for me.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
anoopcr
  • 53
  • 1
  • 6
  • 2
    Or did you want `(vcount === tcount) && (tcount === lcount)` What do you mean by not working. – PSL Oct 18 '13 at 04:03
  • can u post a jsfiddle which shows what is not working – iJade Oct 18 '13 at 04:04
  • Your second code example should be working fine – Phil Oct 18 '13 at 04:05
  • 2
    Your code should work. Better check [difference between == and ===](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons) – Praveen Oct 18 '13 at 04:05
  • 3
    "Not working" is an insufficient description for anyone to know how to help you. – jfriend00 Oct 18 '13 at 04:10
  • Which code is not working? Please improve your question. – Rajesh Paul Oct 18 '13 at 04:58
  • It's unlikely that comparing 3 variables, is actually good business logic/ or implementation practice. I've had 20+ years experience and I feel a smell -- _it doesn't sound like any robust or correct logic I ever heard of._ – Thomas W Oct 18 '13 at 05:08
  • This is the fiddle - http://jsfiddle.net/HKbB2/ and your code works fine. – Akshay Oct 18 '13 at 06:47

5 Answers5

5

consider the following code-

1===1===1

As === operator is left-right associative hence the first part that is 1===1 is evaluated first.

1===1 returns true. then there is the following comparison

true===1

Which is definitely false because boolean true is not same as 1 because === considers value as well as datatype.

So whenever the 3rd parameter is not boolean it will definitely return false if you use === to compare them. Hence you should always use the 2nd syntax to compare any 3 values.

Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57
2

Try with Conditional(Ternary) Operator (?:) which will help. Please refer below example This is one way of alternative solution

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction()
{
document.getElementById("demo").innerHTML= getResult(7,5,5);
}

function getResult(x,y,z){
 return x===y?x===z?true:false:false;
}

</script>

</body>
</html>
Hariharan
  • 3,191
  • 4
  • 19
  • 31
1

Apart from what others have said, verify your variables are the correct type. For example, one might be a character and another other is an integer. Try to verify they are all the same by doing toString(value), or parseInt(value,10).

ryanlutgen
  • 2,951
  • 1
  • 21
  • 31
1

There are multiple ways to do this. I prefer this way to work with n variables

    function isEqual1() {
        var val = arguments[0],
            equal = true;
        for (var i = 0; i < arguments.length; i++) {
            equal = (arguments[i] === val);
            if (!equal) return false;
        }
        return equal;
    }

else

    function isEqual2() {
        var args = Array.prototype.slice.call(arguments, 0);
        var equal = args.join(',').replace(new RegExp(args[0], "ig"), '').replace(/,/g, "");
        return equal.length == 0;
    }

Results

    console.log(isEqual1(1, true, false, "xyz"));
    console.log(isEqual1(true, true, true, true));
    console.log(isEqual2(true, 123, true, true));
    console.log(isEqual2(true, true, true, true));

Demo: http://jsfiddle.net/p5aa7/7/

Exception
  • 8,111
  • 22
  • 85
  • 136
0

A more functional approach is to create a set and than check the size of the set:

const things = new Set(["b","b","b"]);

if (things.size === 1 ){
  console.log('Only one thing!');
}
Twistleton
  • 2,735
  • 5
  • 26
  • 37