0
Can anyone explain this JavaScript code for me?
//this is for switching the players in pig game let activePlayer = 0; activePlayer = activePlayer === 0 ? 1 : 0 ;
2 Respostas
+ 1
First it will check activePlayer===0.
Here it will be true so, here 1 will be assigned to activePlayer using ternary Operator
+ 1
Mohammad
That's ternary operator short of single if else statement.
Here === check type as well as value so here type is number and values are equal so 1 will be output and will be assign to same variable as = used for assignment.
You can write like this also:
if (activePlayer === 1)
console.log(1)
else
console.log(0)