Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if all variables are different?

Is it possible to check if all variables are different from each other in another way than this?

var a = 1, b = 2, c = 3, d = 4;

if(a != b && a != c && a != d && b != a && b != c && b != d && c != a && c != b && c != d && d != a && d != b && d != c){
  //All numbers are different
}

for example

if(a != b != c != d){

}
like image 715
gr3g Avatar asked Dec 04 '25 23:12

gr3g


1 Answers

You can store them all in an object and check if the number of keys is equal to the number of variables used, like this

var dummy = {};
dummy[a] = true;
dummy[b] = true;
dummy[c] = true;
dummy[d] = true;
console.log(Object.keys(dummy).length === 4);

If the values are different, then a new key will be created every time and the number of keys will be equal to the number of variables used.

like image 140
thefourtheye Avatar answered Dec 06 '25 12:12

thefourtheye