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 |
|||||||||||||||||
|
It's generally a bad idea to do variable assignment inside of an
The
The
|
|||||||||
|
It is called operator precedence
In the above example. then results are evaluated against the main && sign.
So 2 && 8 return a = 8 |
|||
|
You're setting (not comparing) |
|||
|
Your statement is interpreted like
when you uses |
|||
|
To understand what is going on, refer to the operator precedence and associativity chart. The expression
Finally, the Note that |
|||||
|
b=8
in parens but nota=2
? It looks almost as if this is a trolling attempt. – Niklas B. yesterdayb=
, why didn't you bother to check the value ofb
? That would have given enough clues... – rvighne yesterdayb=8
or you can simply say, to avoid error. – Jay Shukla 17 hours agoa = (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