I am trying to toggle the display content of the page as well as the text on the button when the button is clicked. The if part of the statement works but not the else. Tried lots of different ways but still don’t understand why it’s not working?
The only time this will not run is when showBtn.textContent is equal to a false value like an empty string.
So, this condition, below, does not make much sense as an if else:
As, the execution will never reach this else if statement, if showBtn.textContent = 'Break Timer' anyway.
So, in terms of why that block is never running will depend on some more code. Do you have a link to a CodePen with more code or similar? With the code I can see:
There is never a case where the first if will not run.
Can you explain why there is never a case the first if will not run? Even if write the below, it still doesn’t work… but why - the text content should have changed?
I tried that but it still doesn’t work - actually the text doesn’t even appear on the button if I use === ? I am so confused…
Codepen here: https://codepen.io/chunzg/pen/ExNEvqE
Yes, at first the assignment and comparison syntax can be a little confusing (some languages intentionally have chosen to not use the same symbol for the two) and even veteran coders sometimes get bitten by the syntax.
One thing that can help is to always use the strict equality === that way not only will you be less likely to mistype but they are also visually more different (=== and =).