How does the below code execute?
if(a=2 && (b=8))
{
console.log(a)
}
OUTPUT
a=8
|
It has nothing to do with the
Here the last one,
And |
|||||||||||||||||
|
|
To understand what is going on, refer to the operator precedence and associativity chart. The expression
Finally, the Note that |
|||||
|
|
It is called operator precedence
In the above example. then results are evaluated against the main && sign.
So 2 && 8 return a = 8 |
|||
|
|
|
Your statement is interpreted like
when you uses |
|||
|
|
|
It's generally a bad idea to do variable assignment inside of an
The
The
|
|||||||||
|
|
You're setting (not comparing) |
|||
|
|
b=8in parens but nota=2? It looks almost as if this is a trolling attempt. – Niklas B. 14 hours agob=, why didn't you bother to check the value ofb? That would have given enough clues... – rvighne 10 hours agob=8or you can simply say, to avoid error. – Jay Shukla 1 hour ago2&&bis not a valid left hand side of an assignment, which means that=has at least lower precedence than&&– Niklas B. 1 hour ago