• Register
1 vote

Problem :

Currently I am learning Javascript. I know the expression false && anything is short-circuit evaluated to false. According to this information I can certainly expect false && true || true must be evaluate to the false. But this is not the case. I saw that expected result false is only given if the above statement is only written as below:

false && (true || true)

I have already done a lot of research on it and I only understood with it is above statement is only evaluated by the order of precedence. So please help me in finding correct solution of below question.

What is the value of the following expression? true || true && false”?

8 5 2
3,230 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution :

I can help you in understanding the short circuit. The use of short circuit is not to waste cycles evaluating the expression at right side only if it can no longer affect your result of your logical operator.
As far as your expression in question is considered it clearly means that your right hand side expression (true) of the false && true will be completely ignored as || true is not the part of your expression.
So now pay close attention this is what will happen in execution:
The logical operator && will be evaluated for the very first time.
Above expression has two expressions on both sides of your && which need to be. evaluated next.
Your first expression (false) and it evaluates to false.
Then your second expression (true) is short-circuited as the logical operator can never result in anything but the false.
The false && true has been evaluated to the false.
Now the logical operator || will be evaluated in next step.
Your first expression (false) is evaluates to false.
And your second expression (true) is evaluates to true.
Now your false || true has been evaluated to true.
And so finally the end result is true.


8 4 1
5,680 points
0 votes


Your confusion all comes down to a misunderstanding of precedence.

A mathematical analogue could be "0 multiplied by anything equals 0." Consider the following expression below:

0 x 100 + 5

In any programming language, or a decent calculator, this evaluates to 5. The "0 times anything" axiom is true - however the "anything" in this case is 100, NOT 100 + 5! For viewing why, compare it to this:

5 + 0 x 100

It doesn't matter whether you include the 5 at the beginning or the end - the operator precedence rules remove ambiguity from the statement.

In JavaScript boolean logic, && has higher precedence than ||. Because each operator is commutative, writing

false && true || true

is exactly the same as writing

true || false && true

You are looking at precedence wrong.

&& is done first, then || so what it looks like is how you wrote it:

(false && true) || true

So the MDN link is correct.

The language performs by parsing an expression into abstract syntax tree. This expression false && true || true gets parsed into like this:

         /  \
       &&    true
      /  \
 false    true

Just after building the AST, the short-circuited evaluation can take place.

The false && anything is short-circuit appraised to false.

quote applies just to a sound sub-tree where false and anything are subtrees of && node, :

      /  \
 false    true

which meansrefers just the false && true gets short-ciruit appraised to false and resulting false || true is evaluated to true

JavaScript will parse this expression :

1) false && true //evaluates to false
2) false || true //equals true. The first false is the result above

It's simple to find how JavaScript parse this whole expression :

(function(){alert(1); return false; })() && 
(function(){alert(2); return true; })() || 
(function(){alert(3); return true; })()


10 6 4
31,120 points

Related questions

0 votes
1 answer 28 views
Problem: I have faced this problem! any help? What is the value of the following expression? true || true && false?.
asked Mar 7 Wafa Abu Yousef 6.1k points
0 votes
1 answer 461 views
Problem: Hello programmers! I am learning to program, and I just got a little confused about boolean values and logical operators. I tried to run the following program: #include<iostream> using namespace std; int main() { bool check = false && true || true; ... program, I got the following output: Condition evaluated to true I am so confused about how this condition evaluates to true. Any help?
asked Dec 18, 2020 Code Learner 9.9k points
0 votes
1 answer 662 views
Problem: Hello Guys, I am kinda new to python I was attempting a quiz and I was not able to attempt this question I think this question is wrongly define,or I am not able to understand. what is the output of the following print statement? print('the path is d:\\sample\\test.'). This was the question that was asked.
asked Jun 5, 2020 Gavin 15.3k points
0 votes
1 answer 3 views
Problem: I am in trouble while solving it .. please help me ..sum of n prime numbers in c
asked Apr 20 Ifra 37.2k points
0 votes
1 answer 3 views
Problem: How to convert colors in RGB format to hex format and vice versa? For example, convert '#0080C0' to (0, 128, 192).
asked May 1 muktaa 34.6k points
0 votes
0 answers 12 views
Problem: How can I catch multiple exceptions at once? True/false: An exception handler is a piece of code that is written using the try/except statement.
asked Mar 29 PkGuy 23.5k points
0 votes
1 answer 8 views
Problem: I'll make a submission for a response to my question. Please, I have searched the internet but have not found any useful material, and I am now having trouble continuing my studies.
asked Apr 1 rakib1 51.5k points
0 votes
1 answer 21 views
Problem: Hello! I am a new programmer. I was attempting a quiz when I came across the following question: If a is true, b is true, and c is false, which of the following expressions is true? The options for this question were: A || B && C A && B && !C A && B || C Two of above I want to know the correct answer to this question along with the complete explanation. Thank You!
asked Apr 25 Code Learner 9.9k points
0 votes
1 answer 10 views
Problem: This is what the console logs (Can be seen in stackblitz link): FILTER VALUE: false IS THE FILTER TRUE? false Returning complete TODOS I have a feeling I'm missing something really simple, but at the same time this just seems wrong ... Thoughts?
asked May 2 sumaiya simi 43.9k points
0 votes
1 answer 26 views
Problem: write an expression that evaluates to true if and only if the value of x is equal to zero.
asked Feb 23 Muneeb Saadii 130k points