+ 3
Confusion.. need a explanation
I do not fully understand the progress of the toggle variable. How can the function toggle between false and true by each time the function is executed? Please explain briefly would be much appericiated👍 toggle=0; function handleToggle(doc) { doc=document.getElementById("btn"); if (toggle==1) { toggle=false; doc.innerHTML=("btn state is False"); console.log("False"); } else { toggle=true; doc.innerHTML=("btn state is True"); console.log("True"); } }
1 Réponse
+ 3
The toggle variable is a Boolean variable so is taking the value true or false. Often these are also seen as being 1 for true and 0 for false.
So the code is setting toggle to 0 (false) initially and then the function does a check to see if toggle equals 1 (true).
If toggle does equal true it changes it to equal false and then writes to the log. if toggle doesn't equal true then it sets it to be equal to true and writes to the log.
Therefore every time the function is run the value of toggle changes from true to false or from false to true.