How do you select by ID and class, and what is returned?

This tests basic DOM API knowledge and awareness of live collections. Use document.getElementById for IDs and document.getElementsByClassName for classes, which returns a live HTMLCollection. A red flag is calling the result a static Array or NodeList.
What's really being asked
This question evaluates foundational DOM API literacy. At the senior level, interviewers still care that you can precisely name the legacy methods and, more importantly, that you understand the behavioral contract of the returned collection. The core concept is the live HTMLCollection versus static NodeLists or Arrays.
The full answer
A strong answer states that document.getElementById accepts a string ID and returns a single Element or null. It then states that document.getElementsByClassName accepts a string of one or more space-separated class names and returns an HTMLCollection. The candidate should explicitly note that this collection is live, meaning it automatically reflects DOM mutations without being requeried. A great answer adds that the collection is array-like but not an instance of Array, so it lacks native map or filter methods unless you borrow them via Array.prototype or convert it with Array.from.
The mistakes people make
A common red flag is saying getElementsByClassName returns a NodeList or an Array. Another is claiming the collection is static, which would lead to bugs during iteration if the DOM changes underneath you. Some candidates only mention querySelector and querySelectorAll, which, while valid for selection, do not answer the specific question asked and miss the opportunity to demonstrate knowledge of live collections. Failing to mention that getElementById returns null when no match exists is also a minor gap.
What usually comes next
The interviewer may ask how you would convert an HTMLCollection into a true Array and why you might need to do so. They might ask about the performance implications of live collections during long loops or whether querySelectorAll returns a live or static collection. Another follow-up is asking what happens when you call getElementsByClassName on an element rather than the document, which restricts the search to descendants.
A concrete example
Imagine a todo list where each item has the class task. You call const tasks = document.getElementsByClassName('task') and then loop through the collection to remove completed items. Because the collection is live, every time you remove a task from the DOM, the HTMLCollection shrinks immediately. If you increment a standard index in a for loop, you will skip the next item. A senior candidate would flag this hazard and suggest iterating backwards or converting to a static array first.
Interview question
Which accurately describes the return values of getElementById and getElementsByClassName?
- a.The first returns a NodeList; the second returns a static Array.
- b.Both return a NodeList that updates automatically when the DOM changes.
- c.The first returns an Element or null; the second returns a live HTMLCollection.Correct
- d.Both return a live HTMLCollection.
Why? this is the answer
getElementById returns a single Element or null, while getElementsByClassName returns a live HTMLCollection that automatically reflects DOM mutations. Option D is tempting because both methods query the DOM, but getElementById never returns a collection.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on dom — each one lists the topics its interview covers.
See open roles