Saturday, November 22, 2025
HomeLanguagesJavascriptHTML DOM TreeWalker lastChild() method

HTML DOM TreeWalker lastChild() method

The TreeWalker lastChild() method moves the current Node to the last visible child of the current node, and returns the last found child. If no such child exists in the document, this method returns null.

Syntax:

node = treeWalker.lastChild();

Parameters: This method takes no parameters.

Return Value:

  • Returns lastChild of the node if exists.
  • Returns null if no such child exists.

Example: In this example, create a TreeWalker with head node and hence showed the lastchild of that TreeWalker node.

html




<!doctype html>
<html>
<head>
    <meta charset="utf-8">
<title>HTML DOM TreeWalker lastChild() method</title>   
</head>
<body>
    <h1>neveropen</h1>
    <p>Click Below</p>
    <button onclick="get()">Click</button>
</body>
<script>
        var treeWalker = document.createTreeWalker(
        document.head,
        NodeFilter.SHOW_ELEMENT,
        { acceptNode: function(node) {
 return NodeFilter.FILTER_ACCEPT; } },
        false
);
        function get(){
            var node = treeWalker.lastChild();
            console.log(node)
        }
</script>
</html>


Output:

Before Button Click:

After Button Click: In console, the last child of the TreeWalker node that is <title> can be seen.

Supported Browsers:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 4 and above
  • Safari 3 and above
  • Opera 9 and above
  • Internet Explorer 9 and above
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6849 POSTS0 COMMENTS