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

1 vote
2.9k views

## 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”?

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.

## Solution:

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; })() &&

## Related questions

28 views
Problem: I have faced this problem! any help? What is the value of the following expression? true || true && false?.
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?
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.
3 views
Problem: I am in trouble while solving it .. please help me ..sum of n prime numbers in c
3 views
Problem: How to convert colors in RGB format to hex format and vice versa? For example, convert '#0080C0' to (0, 128, 192).
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.
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.