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.

8
  • 2
    Or did you want (vcount === tcount) && (tcount === lcount) What do you mean by not working. Commented Oct 18, 2013 at 4:03
  • can u post a jsfiddle which shows what is not working Commented Oct 18, 2013 at 4:04
  • Your second code example should be working fine Commented Oct 18, 2013 at 4:05
  • 2
    Your code should work. Better check difference between == and === Commented Oct 18, 2013 at 4:05
  • 3
    "Not working" is an insufficient description for anyone to know how to help you. Commented Oct 18, 2013 at 4:10

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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>

1 Comment

@FelixKling : agree with u
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).

Comments

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/

Comments

1

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!');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.