Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How does the below code execute?

if(a=2 && (b=8))
{
    console.log(a)
}

OUTPUT

a=8
share|improve this question
7  
Why would you put b=8 in parens but not a=2? It looks almost as if this is a trolling attempt. –  Niklas B. yesterday
    
When you saw b=, why didn't you bother to check the value of b? That would have given enough clues... –  rvighne yesterday
    
@NiklasB. If I don't put b=8 into brackets then it gives me an error because it executes it someting like a = (2 && b=8). I am not clear about this error but the reason to put b=8 into brackets is only give high precedence to b=8 or you can simply say, to avoid error. –  Jay Shukla 17 hours ago
    
@Jay So you already knew that the code parses as a = (2 && (b=8)). This pretty much would have allowed to figure out the problem. At the very least I'd expect a minimal amount of debugging before posting a question on Stack Overflow –  Niklas B. 6 hours ago
add comment

6 Answers

up vote 32 down vote accepted

It has nothing to do with the if statement, but:

if(a=2 && (b=8))

Here the last one, (b=8), actually returns 8 as assigning always returns the assigned value, so it's the same as writing

a = 2 && 8;

And 2 && 8 returns 8, as 2 is truthy, so it's the same as writing a = 8.

share|improve this answer
    
Perfect. Thank you so much for nice explanation. –  Jay Shukla yesterday
3  
With the side effect that b will also be 8, of course. –  CompuChip yesterday
    
@CompuChip - Yep, the assigment assigns 8 to b, but assigning will return the assigned value as well, so that's correct. –  adeneo yesterday
    
"assigning always returns the assigned value" - didn't know this one, thank you. –  Valdas 16 hours ago
add comment

It's generally a bad idea to do variable assignment inside of an if statement like that. However, in this particular case you're essentially doing this:

if(a = (2 && (b = 8)));

The (b = 8) part returns 8, so we can rewrite it as:

if(a = (2 && 8))

The && operator returns the value of the right hand side if the left hand side is considered true, so 2 && 8 returns 8, so we can rewrite again as:

if(a = 8)
share|improve this answer
    
is it because anything greater than 0 is considered true? –  staticx yesterday
    
@staticx Any non-zero integer (so positive or negative) is considered true when converted to a boolean. –  Anthony Grist yesterday
add comment

It is called operator precedence

(a=2 && (b=8))

In the above example. then results are evaluated against the main && sign.

(a=2 && (b=8)) evaluated to a = 2 && 8

So 2 && 8 return a = 8

share|improve this answer
add comment

You're setting (not comparing) a to 2 && (b=8). Since 2 is tru-ish the second half of the expression will be executed, i.e. a = true && (b = 8), i.e. a = (b = 8), i.e. a = 8.

share|improve this answer
add comment

Your statement is interpreted like

a = (2 && (b=8))

when you uses && statement, then last true statement value will be returned. Here (b=8) will becomes value 8 which is true and last statement.

share|improve this answer
add comment

To understand what is going on, refer to the operator precedence and associativity chart. The expression a = 2 && (b = 8) is evaluated as follows:

  • && operator is evaluated before = since it has higher priority
    • the left hand expression 2 is evaluated which is truthy
    • the right hand expression b = 8 is evaluated (b becomes 8 and 8 is returned)
    • 8 is returned
  • a = 8 is evaluated (a becomes 8 and 8 is returned)

Finally, the if clause is tested for 8.

Note that 2 does not play any role in this example. However, if we use some falsy value then the result will be entirely different. In that case a will contain that falsy value and b will remain untouched.

share|improve this answer
    
What makes 2 truthy? –  staticx yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.