Spelling: you’ve called the function is toggleDiplay but you’re trying to call toggleDisplay
This is wrong: addEventListener takes a function as the second argument. If you call the function (toggleDisplay()), that means you are passing whatever value that function evaluates to instead
@DanCouper I see!!! I’ve always wondered why the parentheses are sometimes needed; and sometimes just break everything. Thank you for pointing that out!!
I can remember being really confused as to why that didn’t work when I was first starting out, why I couldn’t do that. It took ages for it to click with me: a function always returns a value (and when there isn’t a return value defined, the return value is undefined). So whenever you see a function is being called, you replace it with whatever the return value is.
This is important to realise, cos it’s how the JS engine works. Program runs:
{Rest of my program}
It reaches that addEventListener function call. That’s what’s happening now, put it on top
addEventListener
{Rest of my program}
Evaluate that. It calls another function. Put that function on top
toggleDisplay
addEventListener
{Rest of my program}
Evaluate that, it’s a value, use that and pop the function off
addEventListener
{Rest of the program}
The event listener can now be evaluated to something. Do that, pop it off
This is the first time this has happened to me (posting a code with a typo) .
I guess there is no IDE or type of practice that can save you from that.
My apologies
@DanCouper Wow thanks for the call stack breakdown! The subtle difference between referring a function and calling it only became apparent to me when I first worked on React. I noticed that when passing a method through props, this.functionName works while this.functionName() gives funky errors. Now I finally know why. Thanks again!