getElementById Quiz
Multiple Choice Easy
Question
What does `document.getElementById('main')` return if no element has id='main'?
Hint
getElementById returns null when no match is found.
Explanation
getElementById returns the element with the matching id, or null if no element has that id. It does not throw an error or return undefined. This is why you should always check the result before using it: const el = document.getElementById('main'); if (el) { ... }.
Related Resources
Related Lessons
- DOM
Learn about getElementById.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.