Is >= equal to > || === or > || == in JavaScript? [duplicate] - javascript

This question already has answers here:
Why `null >= 0 && null <= 0` but not `null == 0`?
(6 answers)
Closed 4 years ago.
Is the below if statement
if (a >= b)
Equal to this?
if (a > b || a === b)
Or is it equal to this?
if (a > b || a == b)

It is equivalent to if(a > b || a == b)
var a = "2";
var b = 2;
console.log(a >= b); // true
console.log(a > b || a == b); // true (== compares value)
console.log(a > b || a === b); // false (=== compares value and type)

You can test it in the console:
var a = 0;
var b = '0';
a == b; // true
a === b; // false
a >= b; // true
Ergo, >= is equivalent to > || ==.

The actual result depend on the use case if the typeof both a & b is same then (a >= b) is same as (a > b || a === b). This is because == is equality with type coercion
var a = "2";
var b = "2";
console.log(a >= b); // true
console.log(a > b || a == b); // true
console.log(a > b || a === b); // true
var a = "4";
var b = 4;
console.log(a >= b); // true
console.log(a > b || a == b); // true
console.log(a > b || a === b); // false

Actually the first one
if(a >= b)
is similiar to
if(a > b || a == b)
but not equals to
if(a > b || a === b)
because in this last one you are even comparing the type of both the operands.
Example:
x = "5"
console.log(x==parseInt(x)) will return true
console.log(x===parseInt(x)) will return false
So, == does not consider the types of operands.

It depends. If before the statement you had defined a or b with a different type,
if(a > b || a === b)
will return false if the first clause is not true. However if you didn't define a or b before both will have the same type and both expressions are equivalent.
You can understand "===" as
(a == b && sameType(a,b))

Related

JS syntax for "if a or b or c but not all of them" [duplicate]

This question already has answers here:
How to xor three variables in javascript?
(5 answers)
Closed 7 months ago.
So, I am super new to JS! I am trying to write a syntax to check if only one of the three variable is present.
if a and (not b or not c) or b and (not a or not c) or c and (not a or not b)
Try this out:
if ((a || b || c) && !(a && b && c)) {
}
Here's a test:
function specialBool(a, b, c, message){
if ((a || b || c) && !(a && b && c)) {
console.log(message)
}
}
specialBool(true, false, false, 'One true');
specialBool(true, true, false, 'Two true');
specialBool(true, true, true, 'All true'); // Shouldn't log
Convert them to booleans, then add them up, and check that the result is 1. (true will be coerced to 1; false will be coerced to 0.)
if (Number(Boolean(a)) + Number(Boolean(b)) + Number(Boolean(c)) === 1) {
// ...
}
or
const sum = [a, b, c].reduce((sum, item) => sum + Number(Boolean(item)), 0);
if (sum === 1) {
// ...
}

Generic rules to build numeric multipliers from truth tables

I have some truth tables, mostly AND, based on 0, 1, -1 which I am using for Math operations:
Example 1:
var a,b,c;
if(a == 1 && b == 1) c = 0;
if(a == 1 && b == 0) c = 0;
if(a == -1 && b == 1) c = 0;
if(a == -1 && b == 0) c = 1;
Example 2:
var a,b,c;
if(a === 1 && b === 1) c = 1;
if(a === 1 && b === 0) c = 1;
if(a === -1 && b === 1) c = 1;
if(a === -1 && b === 0) c = -1;
For instance, the first one could be expressed as follows:
c = (a>>1)*(b-1);
I would like to convert also the second truth table using only Math or Bitwise Operators, how can it be done?
Is there any generic rule to easily create such kind of one-line expression?
You can use a generic superoptimizer such as this one I wrote (unfortunately not very user friendly): https://github.com/falk-hueffner/sematrope
This will give you (a + b) >>> 31 for your first example and ((a + b) | b) & a for the second.
For a more systematic approach, you can convert -1/1 to 0/1 by (x >> 1) + 1, do regular logic operations with |&^, and then (if needed) convert 0/1 to -1/1 by x ^ (x - 1).
If a is always 1 or -1 and b is always 1 or 0, and you're OK to use a boolean operator (||), then
c = b || a
should do it...
const c = (a, b) => b || a
console.log(c( 1, 1)) // 1
console.log(c( 1, 0)) // 1
console.log(c(-1, 1)) // 1
console.log(c(-1, 0)) //-1

jQuery if condition 1 or condition 2 is true than

I am trying to alert "yes" if ether of the conditions in my if statement are true:
var a = 2;
var b = 1;
if (a = 1 or b = 1 ) {
alert('yes');// should alert in this case
} else {
alert('no');
}
https://jsfiddle.net/90z7urvd/1/
What do I use for the if, if this is possible?
a = 1 will set the value 1 to variable a. It is not doing a comparison. For comparison, you use === or ==
=== (Identity operator) is the correct way to compare if both the types are same.
if (a === 1 || b === 1 ) {
=== operator won't do the type conversion before the comparison while == does the type conversion before the comparison.
For your or case, You may use || operator
var bootresul = someExpression || anotherExpression
Corrected code
var a = 2;
var b = 1;
if (a === 1 || b === 1 ) {
alert('yes');
} else {
alert('no');
}
You are assiging value rather then comparing
Try like this
if (a == 1 || b == 1)
To compare strictly use ===
Like this
if (a === 1 || b === 1)
JSFIDDLE
you can do this
var a = 2;
var b = 1;
if ((a == 1) || (b == 1 )) {
alert('yes');// should alert in this case
} else {
alert('no');
}
the == is one of the relational operator for checking equality and || is a logical operator that is a notion of logical OR
use this to compare just values
if (a == 1 || b == 1){
}
OR use this to compare values and type of variable
if (a === 1 || b === 1){
}
note : == will just check of values and === this will check value with type of variable
var a = 2;
var b = 1;
if (a == 1 || b == 1 ) {
alert('yes');// should alert in this case
} else {
alert('no');
}
I think you were doing assignment instead of comparison
Try using this:
if(a === 1 || b === 1){
alert('YES!')
}else{
alert('NO!')
}
OR you can use ternary operator condition instead of if else
(a == 1 || b == 1) ? alert('YES!') : alert('NO!')

Double compare in JavaScript looks weird [duplicate]

This question already has answers here:
chaining double equals == returns false [duplicate]
(3 answers)
Closed 7 years ago.
Let's var a = "first", b = "second", c = "first";.
Expression a == c returns true, (of course!); a == c is true too.
Then why does a == a == c return false?
Example
var a = "first", b = "second", c = "first";
write( a != b != c );
write( a == b != c );
write( a == b == c );
write( a != b == c );
write("");
write( a == a == c );
// -------------------
function write(val) {document.querySelector("console").innerHTML += "<hr noshade size='1px' color='#eee'>" + String(val);}
<console></console>
Because what is really happening is this:
(a == a) == c
Which is really just:
true == "first"
See Operator Precedence.
Because it goes from left to right, one == at a time and the result of each previous == operation is a boolean:
(a == a) === true
(true == c) === false
You need to do:
a == a && a == c
or better yet (since this is JavaScript):
a === a && a === c
Alternatively, can write in CoffeeScript, which will translate:
a == a == c
into
(a === a && a === c);
for you.
a == c returns a boolean (true).
Since a is not a boolean, it cannot possibly equal a == c.

compare three variable using java script

Hi I have three dynamic variable
my below code is working to check if all are equal
if ((a == b) && (b == c)) {
// they're all equal ...
}
But, I want to create a function passing three variable to check if any one variable is equal to other variable.
a=1;b=2;c=1;
isEqual = compareVariable(a,b,c);
here isEqual should be true .
How to create this function
function compareVariable(a,b,c) {
return a==b || b==c || c==a;
}
Try using it like this:
function compareVariable(a,b,c){
var a = a;
var b = b;
var c = c;
if ((a == b) && (b == c)) {
return true
}
else
{
return false;
}
}
compareVariable(1,2,3);
function compareVariable(a,b,c)
{
if ((a == b) || (b == c) || (a == c) ) {
alert("equal");
// they're all equal ...
}
else
{
alert("notequal");
}
}
Try this:
function compareVariable(a,b,c){
if ((a == b) || (b == c) || (a == c)) {
return true
// any or all equal
}
else
{
return false;
// if none of two is equal
}
}
To create a function you need to use the reserved keyword 'function' followed by the name of the function. In this case:
function compareVariable(a,b,c) {
return ((a == b) || (b == c) || (a == c));
}
This function will take your three variables and return boolean true if all the variables are the same.

Categories

Resources