Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:23:55+00:00 2026-06-13T21:23:55+00:00

I am trying to understand how the recursion works. The code below walks through

  • 0

I am trying to understand how the recursion works. The code below walks through the nodes of the HTML document. It invokes a function, passing it each node in turn. can someone please explain how it is being done in steps? thanks.

var walk_the_DOM = function walk(node, func) {
    func(node);
    node = node.firstChild;
    while (node) {
        walk(node, func);
        node = node.nextSibling;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:23:56+00:00Added an answer on June 13, 2026 at 9:23 pm

    The first statement is the function definition:

    var walk_the_DOM = function walk(node, func) {
        ...
    };
    

    This assigns a function to walk_the_DOM. This function takes in two parameters: node, and func. node is the node you want to work on and func is the function you want to apply on node.

    The first line of the function is func(node);. This essentially means you are applying a passed-in function on node. For example, if you called walk_the_DOM like this:

    walk_the_DOM(root, function(node) {
        console.log(node);
    });
    

    You would be calling

    function(node) {
        console.log(node);
    }
    

    on every node, which as the effect of printing out every node in the tree.

    The next line node = node.firstChild; basically reassigns the node to its first child. The reason you need to do this is because you need to look at each child of the current node. Of course, you also need to look at the children of those children, but we’ll get to that part later.

    Now we get to the while loop. The condition on this while loop is just while(node), which just means that the loop will run as long as node is not null or undefined. In our previous statement we did node = node.firstChild. What if the current node has no children? Then node.firstChild will be null and so we won’t even enter the loop. We will fall out of it and exit the function (remember this part; we exit the function if the current node has no children. This is know as the stopping condition of our recursive function).

    Now inside the while loop, we make our recursive call: walk(node, func);. Let’s ignore what happens here for a second and move onto the next line: node = node.nextSibling;. Here we’re assigning the next sibling of the node back into the variable node. In effect we are iterating over the siblings of this node. Now what if the node has no other sibling (i.e., the parent node only has one child)? Then node will be null and we will fall out of the loop.

    Now let’s get back to the recursive call walk(node, func). In the recursive call we call the function itself, which means that the behavior of the function is exactly the same as it was for this iteration. You might think at this point “But doesn’t that mean that this will execute forever?”. But it won’t! Why? Remember the stopping condition I mentioned earlier? At some point you will pass in a node that has no children which means that the recursive call will exit and come back to the next line (node = node.nextSibling) and execution will proceed normally. Now if you think of the DOM as a tree (which it is), what this means is that you will travel as far down one branch as you can and once you reach the end, you fall back up one level, and check to see if there are any other siblings. If there are, you go down that branch as far as you can go. If not, you fall back up one more level and do the check again. In this way, you are able to traverse the entire DOM tree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand the recursion call in the below code snippet. static
I'm really trying to wrap my brain around how recursion works and understand recursive
I'm trying to understand within my lesson how to read the below function called
I'm trying to understand how recursion works in C. Can anyone give me an
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which
I'm trying to understand what is the idiomatic way in Clojure to recurse through
So I'm trying to understand how Datalog works and one of the differences between
So I'm currently trying to grasp the concept of recursion, and I understand most
I am trying to understand how stack allocation and alignment works with pthreads on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.