Question · JavaScript and TypeScript course · Module 8: Events and the DOM
Checking that an element exists
Why is it worth checking that an element exists before calling addEventListener?
Answer options
- Because
getElementByIdmay returnnull, which throws an error when adding a listener - Because addEventListener only works on images
- Because otherwise the page won't load
- Because listeners can be added only once in the whole app
You can check the correct answer by solving this task in the CodeWorlds game.