How does the below code execute?
if(a=2 && (b=8))
{
console.log(a)
}
OUTPUT
a=8
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
2 is evaluated which is truthyb = 8 is evaluated (b becomes 8 and 8 is returned)8 is returneda = 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.