LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 1

childNodes and children Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

The page contains this HTML:

<ul id="menu">
  <li>Home</li>
  <li>Courses</li>
</ul>

What does the following JavaScript code print when it runs on this page?

main.js
const menu = document.getElementById("menu");
console.log(menu.childNodes.length, menu.children.length);
  • A 2 2
  • B 3 2
  • C 2 5
  • D 5 2 Correct
Correct Answer: Option D

The line breaks before, between and after the two li elements are three text nodes: 3 + 2 = 5 childNodes, but only 2 element children.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links