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 ;

8th Aug 2021, 8:08 AM
Mohammad
Mohammad - avatar
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
8th Aug 2021, 8:31 AM
J Santhosh Kumar
J Santhosh Kumar - avatar
+ 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)
8th Aug 2021, 8:49 AM
A͢J
A͢J - avatar