JavaScript Intro to DOM (querySelector & textContent) Quiz
Answer 40 questions covering DOM fundamentals, document structure, element selection with querySelector/querySelectorAll, CSS selector usage, reading and updating textContent, handling null results, safe DOM manipulation, and best practices.
Question 1
What is the DOM?
Question 2
What does the document root node typically represent in browsers?
Question 3
Which property references the top-level document object?
Question 4
What does document.body return?
Question 5
How are DOM nodes related to each other?
Question 6
Which method finds the first matching element by CSS selector?
Question 7
What does document.querySelectorAll(".item") return?
Question 8
Which CSS selector targets an element by ID?
Question 9
What does this code log?
const title = document.querySelector('h1')
console.log(title)
console.log(title ? title.textContent : 'missing')Question 10
How do you iterate over querySelectorAll results?
const items = document.querySelectorAll('.item')
items.forEach(node => console.log(node.textContent))Question 11
Which code handles missing elements gracefully?
const subtitle = document.querySelector('.subtitle')
if (subtitle) {
console.log(subtitle.textContent)
}Question 12
What does this CSS selector match?
Question 13
How do you select the element with attribute data-role="dialog"?
Question 14
What does querySelectorAll return when no matches exist?
Question 15
What is the difference between textContent and innerText?
Question 16
Which property updates an element’s textual content without parsing HTML?
const message = document.querySelector('#message')
message.textContent = 'Updated!'Question 17
How do you append additional text using textContent?
const statusEl = document.querySelector('.status')
statusEl.textContent += ' - Complete'Question 18
What does element.textContent = "" do?
Question 19
How do you safely update text on an element that might be null?
const alertBox = document.querySelector('.alert')
if (alertBox) {
alertBox.textContent = 'Saved!'
}Question 20
Which scenario favors textContent over innerHTML?
Question 21
What does this code output when the element has nested spans?
<p id="info">Hello <span>World</span></p>
const el = document.querySelector('#info')
console.log(el.textContent)Question 22
How do you select the first <li> inside a .nav list?
const firstItem = document.querySelector('.nav li')Question 23
What is a common pattern for caching DOM references?
Question 24
Why should you store NodeList results before manipulating them?
Question 25
What is printed here?
const nodes = document.querySelectorAll('.item')
for (const node of nodes) {
console.log(node.textContent.trim())
}Question 26
Which code updates multiple elements with the same text?
document.querySelectorAll('.price').forEach(node => {
node.textContent = '$9.99'
})Question 27
How do you ensure an element exists before using it in multiple functions?
Question 28
Which code snippet demonstrates reading textContent safely with optional chaining?
const subtitleText = document.querySelector('.subtitle')?.textContent || ''Question 29
Why should you avoid direct innerHTML assignments when only text needs updating?
Question 30
What is a safe approach when manipulating user-generated text?
Question 31
How do you avoid layout thrashing when updating multiple elements?
Question 32
Which pattern avoids repeated DOM queries inside loops?
Question 33
What is printed if querySelector finds no match?
const node = document.querySelector('.missing')
console.log(node)Question 34
How do you convert a NodeList to an array?
Question 35
Which strategy prevents errors when manipulating optional elements?
Question 36
Why is storing DOM references in module scope useful?
Question 37
What does document.querySelector("div.highlight span") select?
Question 38
Why might you store query results in const instead of let?
Question 39
What best describes the DOM rendering flow regarding textContent updates?
Question 40
What is a recommended best practice for DOM manipulation?
