Tuesday, June 16, 2026
HomeLanguagesJavascriptD3.js selection.empty() Function

D3.js selection.empty() Function

The d3.empty() function in D3.js is used to return a boolean value. The value is true when the selection contains no elements and it returns false when the selection is non-empty.

Syntax:

selection.empty();

Parameters: This function takes no parameters.

Return Values: This function returns a boolean value.

Below given are a few examples of the function given above.

Example 1:




<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport"
            path1tent="width=device-width, 
                    initial-scale=1.0"> 
    <title>Document</title> 
</head> 
<style> 
div{
    background-color: green;
    margin-bottom: 5px;
    padding: 10px;
    width: fit-content;
}
</style> 
<body>  
    <div>Some text</div>
    <div>Geeks for neveropen</div>
    <div>Geeks for neveropen</div>
    <div>Some text</div>
  <script src = 
  </script>
  <script src=
</script>
  <script>
      let selection=d3.selectAll("div")
      console.log(selection.empty())
  </script> 
</body> 
</html>


Output:

Example 2:




<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport"
            path1tent="width=device-width, 
                    initial-scale=1.0"> 
    <title>Document</title> 
</head> 
<style> 
div{
    background-color: green;
    margin-bottom: 5px;
    padding: 10px;
    width: fit-content;
}
</style> 
<body>  
    <div>Some text</div>
    <div>Geeks for neveropen</div>
    <div>Geeks for neveropen</div>
    <div>Some text</div>
  <script src = 
  </script>
  <script src=
</script>
  <script>
    // Selection is empty because no
    // container name h2 is rendered.
      let selection=d3.selectAll("h2")
      console.log(selection.empty())
  </script> 
</body> 
</html>


Output:

RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS