Javascript compare 3 values
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
}
What about
(new Set([a,b,c])).size === 1
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
}