JS: why does this return true? - javascript

0 + 1 === 1 || 1 + 1 === 2 || 2 + 1 === 10
Can someone please explain why this statement returns true?
Individually, the first two are true and the last one is false. However OVERALL, the statement returns true. It seems that you just need one true statement among a longer list of statements to make the OVERALL statement return true. Is that correct?
Just trying to cement my understanding of Booleans. Cheers.

Well, || returns the first truthy value if there is one so:
1 || ANYTHING_IN_THE_WORLD ; // returns 1
So all you really have there is
0 + 1 === 1
Which is true. So yes, your understanding is correct.
Expanding on execution order:
Operator precedence is in play here:
0 + 1 === 1 || 1 + 1 === 2 || 2 + 1 === 10
First we have addition:
1 === 1 || 2 === 2 || 3 === 10
Then we have equality checks:
true || true || false;
Which as explained above true || ANYTHING_AT_ALL is true, so the final output is true.

|| is an or operator. So if condition one or condition two or condition 3 is true, the statement is true.
I suggest reading up on JavaScript here: https://developer.mozilla.org/en/docs/Web/JavaScript

it returns true because with OR operator (||) first true will return true.
The evaluation will perform :
> 1- is (0+1===1)
=> true! Then returns true.
This
> 0+1===1 || 1+1000===1
will return true for the same reason.

"some apples are purple OR some apples are green" is true because it is sufficient that some apples are green. a OR b will be true when either or both are true. a AND b needs both to be true.
Thus, the fact that some of the items in your expression are true makes the whole expression true. It would not be if you replaced || with &&.
In fact,
It seems that you just need one true statement among a longer list of statements to make the OVERALL statement return true. Is that correct?
is (almost) exactly correct. The only correction I'd put in is replacing "true" with "truthy", at least in the context of JavaScript. For example, 3 || 5 is not true, but 3 - while 3 is not true, it is truthy.

Related

Why does this short circuit evaluation return undefined if first value is false?

I have an example similar to this where the first expression evaluates to false and the second is undefined but the overall expression returns undefined into valueResult, shouldn't the first false value terminate the check and return false?
valueResult = false && 10 === 5 ? 'match' : undefined
I have added console log statements and a debugger and this is what is happening, however when I do false && undefined on the browser console, it returns false.
In your updated example, the logical AND … && … has a higher order of precedence (5) than the evaluation of the ternary … ? … : … (3).
Check out the table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table
&&-operator has higher precedence then the ternary operator. That's why following is happening:
Basically you have a ternary operator (false && (10 === 5) ? : 'match' : undefined. 10 === 5 evaluates to false and false && false also results in false. Thats why undefined and not 'match' is returned.
To fix this, add parentheses after && like this:
false && (10 === 5 ? 'match' : undefined)

Javascript short circuiting in if statement

I am confused about the below if statement code. Not sure what it is exactly doing
if (this.props.filterURL && nextProps.filterURL !== this.props.filterURL) {}
Can someone please help me to understand this?
Is it a short-circuiting in if statement:
i.e
1- if first this.props.filterURL from left side is false then it will return false. 2- if first this.props.filterURL has a value then it will return true and the second variable nextProps.filterURL will be compared to this.props.filterURL on the right most of the statement?
Notes :
This short-circuiting is good for the performance, as it allows significant bits of calculations to be skipped.
The AND operator (&&) returns true if both expressions are true, otherwise it returns false.
DEMO
var x = 1;
var y = 5;
if (x > 0 && y < 6) { console.log(x) }; // true
if(x > 1 && y < 6) { console.log(x) }; // false
As suggested by nnnnnn in his comment, the first this.props.filterURL is checking for a truthy value, not for true specifically. If the property is not defined that is falsy, but if the value is null or 0 or an empty string those are all falsy too.
In case of AND operator it evaluates the second expression only if the first one is true.
In your case,
if (this.props.filterURL && nextProps.filterURL !== this.props.filterURL) {}
can be interpreted as if(expr1 && expr2)
where expr1= this.props.filterURL and expr2 = nextProps.filterURL !== this.props.filterURL
for first expression expr1 it evaluates whether it is not null or undefined...
for the second expression expr2 nextProps.filterURL !== this.props.filterURL it checks both the value and data type. So for example if you have both value as same 1234 but for one it is of type String and for another it is for number, this condition would be true.

javascript or operation returns unexpected result

I am very new to javascript. I have a programm like:
var a = 2
if(a !=2 || a != 3){
console.log("not")
}
Here I have set the variable a value to 2.
In my condition I have set if a is not 2 or a is not 3 it should print not
But here a's value being 2 it is printing not.
It always give not whatever the value is
I can check this is python easily like:
if not a == 2 or not a== 3
Whats wrong in here ??
var a = 2
a is now 2.
a !=2
This is false, so we look on the other side of the or
a != 3
This is true. So one of the two sides of the or test is true. Therefore the or test is true.
If you want to test if a value is not 2 and it is also not 3, then you need to use an && operator.
Maybe you want this:
if(a !=2 && a != 3){
console.log("not")
}
In other words, if a is not 2 AND a is not 3.
This is just how boolean logic works. In your example, because a does not equal 3, the condition is true.
Please read how logical operators work.
Your condition evaluates to true if a is not 2 or a is not 3, that means that is will be false only when a is both 2 and 3 at the same time which isn't possible - this condition will always be true.
You might be also interested in reading something on mathematical logic and negations.
If you want to ensure that a is not 2 or 3 then you should change it to
if((a != 2) && (a != 3)){
as in a is not 2 and a is not 3 - keep in mind that you are working with 2 statements.
Simple explanation
false || true = true
a != 2 //returns false
a != 3 //returns true
In python:
not a == 2 // returns false
not a == 3 //returns true
"OR" in the condition means that the statement in the if block will get executed if any one of the equations out of
(a != 2)
and
(a != 3)
returns true.
In your code snippet
(a != 3)
is true and hence the statement
console.log("not");
gets executed.
This is very expected behaviour and it work same in python and javascript
a = 2
if not a == 2 or not a== 3:
print ("not")
is equivalent to
var a = 2
if(a !=2 || a != 3){
console.log("not")
}
in or expression conditions check while one of theme be come true or until there was no condition.

OR operator with real numbers

Why is the mid number result of this statement?
(0 || 0.571428571428571 || 1) == 0.571428571428571
How the comparsion works?
Well. Looks like stupid question. For those who works in php where this statement has a different result could be helpfull to know that JS will return number instead bool.
|| is a short-circuiting operator. It evaluates its left-hand operand, and returns that if it's truthy, otherwise it evaluates and returns its right-hand operator. When you have a sequence of || operators, it performs this left-to-right, returning the first truthy value (or the last value if none are truthy).
In your example, 0 is falsey, so 0.571428571428571 is the first truthy value, and it's returned.
Your expression is interpreted as
((0 || 0.571428571428571) || 1)
The result of the first || is truthy, so that's the result of the overall expression. That's how || works: the result is the left-hand operand if it's truthy, or else the right-hand operand.
The answer to this lies in the fact that 0 is considered a false value while non-zero numbers are considered true.
Since || and && will short circuit and return as soon as it knows the outcome. For OR expressions this means that the first truthy value will be returned. For AND expressions it means the first false value is returned:
(true && true && 0 && true) === 0
(false && false && 1) === 1
(false || false || 1 || false) === 1
(false || false || 0) === 0
Since the first value in your expression is a falsey value, it is not returned but the floating point is a truthy value, so it is returned.

Unable to see the correct value by using && operator [duplicate]

This question already has answers here:
Javascript AND operator within assignment
(7 answers)
Closed 7 years ago.
hi i am preparing for js interview.
i saw this question.
if var a=2, var b =3 What would be the value of a&&b?
The answer given is 3.
I am not able to understand why is this the answer.
Can you help..
Thanks.
&& and || do not create true or false. They return one of the operands.
The binary operators do NOT behave like other c based languages. See more about truthy and falsey values here
&& is defined as follows.
If the first operand is truthy then the result is the second operand. Otherwise it is the first.
Examples
false && true // false the first operand is falsey
0 && true // 0. 0 is a falsy value so the first value is used
1 && 0 // 0. The first value is truthy
// so the second value is used
'hello' && 'world' // 'world' the first value is truthy
// so yield the second value.
|| is defined as follows.
If the first operand is truthy then the result is the first operand. Otherwise it is the second.
Examples
false || true // true, first value is falsey so yield second
0 || true // true, first value is falsey so yield second
1 || 0 // 1, first value is truthy use it.
'hello' || 'world' // 'hello', first value is truthy so yield it.
&& and || are "Short-circuit"
This means that there are ways to structure code such that not all expressions are evaluated. This can be convienient at times but is double edged.
function launchNukes() { /* TODO */ }
0 && launchNukes(); // nukes do not fire
1 && launchNukes(); // nukes fire
0 || launchNukes(); // nukes fire
1 || launchNukes(); // nukes do not fire
&& is an AND operator, just like most everywhere else. Most languages, JavaScript included, will stop evaluating an AND operator if the first operand is false.
You can read it like that:
if a is true , return value will be b
if a is false , return value will be a
So && return values of operands not false and true.
for your example,
2 && 3 // return 3 because 2 is true

Categories

Resources