chris97ong
chris97ong

Reputation: 7060

Javascript - Can there be 3 or more '===' in a single statement?

So, for example, will

function myFunction() {
    if(var_1 === var_2 === var_3 === var_4) {
        return true;
    } else {
        return false;
    }
}

return true or false when

var_1 = true;
var_2 = true;
var_3 = true;
var_4 = true;

Also, what will the function 'myFunction' return when

var_1 = false;
var_2 = false;
var_3 = false;
var_4 = false;

and

var_1 = "abc";
var_2 = "abc";
var_3 = "abc";
var_4 = "abc";

Thanks a lot!

Upvotes: 0

Views: 103

Answers (3)

Karan Kular
Karan Kular

Reputation: 9

In all 3 condition the function will return true..

yes there can be 3 '===' in a single statement

Upvotes: -1

Nick
Nick

Reputation: 6025

No, because the first === will equate to true (or false) and will not equal the next variable.

So var_1 === var_2 will either be true or false.

It then compares that result to var_3 like this true/false === var_3. That gives a result of true or false. That second result is then compared to var_4.

So this will be true according to your test:

var_1 = "yes";
var_2 = "apple";
var_3 = "amazing";
var_4 = false;

I presume that's not what you want :)

Upvotes: 3

Blender
Blender

Reputation: 298176

The case with three variables will be easier to work with. It'll be interpreted as:

(a == b) == c

Which will be:

(some boolean) == c

That's probably not what you intend to happen. To work around this, use the transitivity of equality:

(a == b) && (b == c)

Upvotes: 2

Related Questions