Javascript compare 3 values

javascript compare multiple values
javascript
javascript string comparison
javascript string contains multiple values
javascript check if multiple variables are equal
javascript equals method
how to compare two strings in javascript if condition
javascript or

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.

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
}
```

How to compare 3 values in Javascript?, Can't figure out how to compare 3 values that only 2 value are equal. Greatly appreciate your help! /* Tasks: 1. ask for 3 numbers from user. 2. I'am doing an exercise where I have to compare 3 values and printout what kind of triangle it was. So far this are my code. The last one is tricky. Can't figure out how to compare 3 values that only 2 value are equal. Greatly appreciate your help! /*

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
}
```

JavaScript basic: Check three given numbers, if the three numbers , are same return 40. ES6 Version: function three_numbers(x, y, z) { if (x == y && y == z) { return 30; } if (x == y || y == z || z == x) { return 40; } return 20; } console. Comparing Different Types. Comparing data of different types may give unexpected results. When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. An empty string converts to 0. A non-numeric string converts to NaN which is always false.

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 :)

Difference Between =, ==, and === in JavaScript [Examples], What is the difference between == and === in JavaScript? A Better Way to Perform Multiple Comparisons in JavaScript Having to compare a value with a bunch of other values is a common, even trivial task for a developer. It’s something you probably don’t even think about when you have to do it.

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<len; i++) {
if(arguments[i] == null) {
return false;
}

if(obj == arguments[i]) {
continue;
} else {
return false;
}
}

return true;
}
```

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

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

Comparison operators, is used for comparing two variables, but this operator also checks datatype and compares two values. It returns true only if both values and data types are the same for the two variables. When comparing values of different types, JavaScript converts the values to numbers. For example: alert( '2' > 1 ); // true, string '2' becomes a number 2 alert( '01' == 1 ); // true, string '01' becomes a number 1

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!
}
```

Which equals operator (== vs ===) should be used in JavaScript , compares internal references which are equal when operands refer to the same object in memory. JavaScript has both strict and type–converting comparisons. A strict comparison (e.g., ===) is only true if the operands are of the same type and the contents match. The more commonly-used abstract comparison (e.g. ==) converts the operands to the same type before making the comparison. For relational abstract comparisons (e.g., =), the operands are first converted to primitives, then to the

Equality comparisons and sameness, does differ from the == operator. Lets take 2 vars a and b. For "a == b" to evaluate to true a and b need to be the same value. JavaScript provides three different value-comparison operations: ===- Strict Equality Comparison ("strict equality", "identity", "triple equals") ==- Abstract Equality Comparison ("loose equality", "double equals") Object.isprovides SameValue (new in ES2015).

A Better Way to Perform Multiple Comparisons in JavaScript, JavaScript provides three different value-comparison operations: === - Strict Equality Comparison ("strict equality", "identity", "triple equals") == - Abstract Equality Comparison ("loose equality", "double equals") In Js how do I compare two values. i have got three buttons that each generate a number between 1 and 3. I want to compare the number they output with the number of the button, for example the first button could be 1, the second 2, the third 3. If the number generated by the button is the same as 2, it says its a draw.

Simple and clean way of comparing three numbers, Having to compare a value with a bunch of other values is a common, This might be okay with 3 statements like we have up here but the  Three most accurate ways to compare strings correctly in JavaScript. The == operator has its limitations here because Java is not designed to support it. Why the == operator doesn’t work in JavaScript. The explanation is simple; Java wasn’t built to work with == as an option to compare strings.

• You do not have to check all the variables if they are `!= null`.. If they are equal between them, you can check only one against `null`. The same with checking `f == h`.. if both `f` and `h` are equal to `g` you do not have to check those two ..
• You could even omit the `!= null`
• @m90 buy what if all of them are null? then it will evaluate to `true`.
• If all of them are null `Boolean(g)` will be `false`: jsfiddle.net/SFDRK Or am I missing something?
• @m90 true, but that also goes for all falsy values, the OP’s asked specifically for `null` jsfiddle.net/SFDRK/1