How do you compare three variables in javascript?

Compare 3 Values in JavaScript #

To compare 3 values, use the logical AND (&&) operator to chain multiple conditions. When using the logical AND (&&) operator, all conditions have to return a truthy value for the if block to run.

Copied!

const value1 = 10; const value2 = 10; const value3 = 10; if (value1 === value2 && value2 === value3) { // 👇️ this runs console.log('✅ all 3 values are equal'); } else { console.log('⛔️ values are NOT equal'); }

In the code example, we used the logical AND (&&) operator to chain two conditions.

When using the logical AND (&&) operator in an if statement, all conditions have to be met for the if block to run.

The logical AND (&&) operator returns the value to the left if it's falsy, otherwise it returns the value to the right.

The falsy values in JavaScript are: false, null, undefined, 0, "" (empty string), NaN (not a number). All other values are truthy.

This means that if the conditions on both sides of the logical AND (&&) operator evaluate to a value other than the aforementioned 6, the if block is run.

If our example we check if the 3 values are equal. Since the conditions on both sides of the operator return true, the if block is run.

First, the condition on the left of the && is evaluated, if it returns a falsy value, the condition on the right-hand side is not evaluated at all and the else block is run.

Let's look at another example.

Copied!

const value1 = 10; const value2 = 10; const value3 = 20; if (value1 === value3 && value2 < value3) { console.log('✅ both conditions are true'); } else { // 👇️ this runs console.log('⛔️ at least 1 condition is false'); }

In this example, the condition on the left-hand side is evaluated.

It checks if 10 === 20, which returns false. The AND (&&) operator short-circuits and returns false, and the else block is run.

Further Reading #

  • How to count the words in a String in JavaScript
  • How to use shorthand for if/else statement in JavaScript

I have 3 values that I want to compare f, g and h. I have some code to check that they all equal each other and that none of them are null. I've had a look online but could not find anything that seemed to answer my query. Currently I am checking the code in the following way...

if(g == h && g == f && f == h && g != null && f != null && h != null)
{
//do something
}

This is quite long winded and I might be adding more values, so I was just wondering if there is a quicker way to check that none of the values are null and that all the values equal each other?

Thanks in advance for any help.

asked Apr 2, 2012 at 8:46

5

You could shorten that to

if(g === h && g === f && g !== null)
{
//do something
}

For an actual way to compare multiple values (regardless of their number)
(inspired by/ simplified @Rohan Prabhu answer)

function areEqual(){
   var len = arguments.length;
   for (var i = 1; i< len; i++){
      if (arguments[i] === null || arguments[i] !== arguments[i-1])
         return false;
   }
   return true;
}

and call this with

if( areEqual(a,b,c,d,e,f,g,h) )
{
//do something
}

answered Apr 2, 2012 at 8:52

Gabriele PetrioliGabriele Petrioli

185k34 gold badges254 silver badges306 bronze badges

6

What about

(new Set([a,b,c])).size === 1

answered Nov 19, 2020 at 20:18

olidemolidem

1,76118 silver badges40 bronze badges

5

Works for any number of items.

ES5

if ([f, g, h].every(function (v, i, a) {
  return (
    v === a[0] &&
    v !== null
  );
})) {
  // Do something
}

ES2015

if ([f, g, h].every((v, i, a) => 
  v === a[0] &&
  v !== null
)) {
  // Do something
}

answered Sep 25, 2015 at 19:01

How do you compare three variables in javascript?

JMMJMM

25k3 gold badges48 silver badges55 bronze badges

0

I suggest you write a function where you give an array with all the values you want to compare and then iterate through the array to compare the values which each other:

function compareAllValues(a) {
     for (var i = 0; i < a.length; i++) {
         if (a[i] === null) { return false }
         for (var j = 0; j < i; j++) {
            if (a[j] !== a[i]) { return false }
         }
     }

     return true;
}

that should be it, I think :)

answered Apr 2, 2012 at 8:52

1

Write a simple function:

var checkAllArguments = function() {
    var len = arguments.length;
    var obj;

    if(len == 0) {
        return true;
    } else {
        if(arguments[0] == null) {
            return false;
        } else {
            obj = arguments[0];
        }
    }

    for(var i=1; i

Now, to check multiple arguments, all you have to do is:

if(checkAllArguments(g, h, f)) {
   // Do something
}

answered Apr 2, 2012 at 8:52

Rohan PrabhuRohan Prabhu

6,9665 gold badges36 silver badges66 bronze badges

Create an array of string and check the existance of next value in there

compareAnswers(a1: string, a2: string, a3: string): boolean {
   this.compareAnswerArr.push(a1);
if (this.compareAnswerArr.includes(a2) == false) {
  this.compareAnswerArr.push(a2);
  if (this.compareAnswerArr.includes(a3) == false) {
    return true;
  } else return false;
}
else return false;}

answered Sep 30, 2019 at 17:16

Add a distinct function to Array.prototype:

Array.prototype.distinct = function() {
    var result = [];
    for(var i = 0; i < this.length; i++) {
        if (result.indexOf(this[i]) == -1) {
            result.push(this[i]);
        }
    }
    return result; 
}

Then do:

var myArray = [f, g, h];
if (myArray.indexOf(null) == -1 && myArray.unique().length == 1)
{
    // no nulls and all elements have the same value!
}

answered Dec 7, 2016 at 13:27

KrisKris

39.2k9 gold badges73 silver badges100 bronze badges

3

How === works in JavaScript?

Strict equality using === Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values have different types, the values are considered unequal.

Should I use == or === in JavaScript?

= Vs == VS === in JavaScript = in JavaScript is used for assigning values to a variable. == in JavaScript is used for comparing two variables, but it ignores the datatype of variable. === is used for comparing two variables, but this operator also checks datatype and compares two values.

What is === in JavaScript example?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

Why we use == and === in JavaScript?

= is used for assigning values to a variable in JavaScript. == is used for comparison between two variables irrespective of the datatype of variable. === is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.