Start and Stop Button
Greetings, everyone! As I was working on my last code, I came across something very interesting. Take a look at this: <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <button>Start</button> <script> let butn = document.querySelector("button"); let boolean = false; butn.addEventListener("click", ()=>{ if (boolean == false){ butn.innerHTML = "Stop"; boolean = true; } else{ butn.innerHTML = "Start"; boolean = false; } }); </script> </body> </html> Honestly, it would have taken me a long time to figure this out by myself, if at all. This code has a boolean as a variable and is somehow able to affect the button, creating a toggle function. What I don't understand is the connection between the boolean and the toggle function. Can someone explain to me, in detail, how is everything connected, logically? What is the connection between the boolean variable and the button? How, by adding an extra variable, is it logically possible to affect the function and result in a toggle? I can see that it works perfectly, but I don't understand why?