0

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.

5
  • 1
    honestly, how are we supposed to support you there? You are right - short circuit should definitely return false. Please provide your real data or anything Commented May 31, 2022 at 18:35
  • I've updated the code to match more closely to what I'm seeing. The actual code was on another machine so I'm giving an identical example Commented May 31, 2022 at 18:37
  • 2
    Your code basically works like (false && 10 === 5) ? 'match' : undefined Commented May 31, 2022 at 18:39
  • Thanks I've got it now, how do you know that implicit grouping is made with parentheses? Commented May 31, 2022 at 18:49
  • because of precedence... developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 31, 2022 at 19:07

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

The ternary contains a === check which is higher priority than the && though. Either way, in theory the first value of false should be returned right? are there any scenarios where it wouldn't and undefined would come back?
yes, === has a higher precedence, but &&-operator is still higher than the ternary operator, that's the whole point! What you want is this: false && (10 === 5 ? 'match' : undefined). But what you get is this: (false && 10 === 5) ? 'match' : undefined. Again: it has nothing to do with the === operator
Thanks, thats better, you have the right number of parentheses there so it makes more sense now.
Ah, I think I see the source of confusion here, @j-obe. The ternary operator has right-to-left associativity -- check out the 'conditional chains' example for it on MDN to see why it returns undefined in your example; the && is basically short-circuited.
1

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

8 Comments

Not sure if the question has been misread, I don't want 'match' returned, I'm wondering why undefined is returned when the first value before the && is false
No it's not missread. I just broke down the evaluation. Just add parentheses and it's fixed
Ok, maybe I have missed the bit with the answer, I'm not sure why undefined is being returned in my example based on what you've explained
Because of precedence. && Has higher precedence than ternary operator
I understand based on a comment someone else has made now, I think you're missing a parentheses in your answer which confused me with your explanation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.