Variable assignment inside an 'if' condition in JavaScript

前端 未结 6 709
眼角桃花
眼角桃花 2020-12-29 19:59

How does the below code execute?

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

OUTPUT

a=8
6条回答
  •  没有蜡笔的小新
    2020-12-29 20:22

    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.

提交回复
热议问题