Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Display the number of links present in a document using JavaScript

  • Difficulty Level : Basic
  • Last Updated : 19 Feb, 2019

Any webpage that is loaded in the browser can be represented by the Document interface. This serves as an entry point to the DOM tree and the DOM tree contains all elements such as <body> , <title>, <table> ,<a> etc.

We can create a Document object using the Document() constructor. There are many properties of these Document object. One of such property is links. The links property gives us a collection of all <area> elements and <a> elements in a document. One more property is the length property. The length property tells us the number of links present in the document.links array. So, the document.links.length statement gives us the number of links present in a document.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Below HTML document contains JavaScript piece of code which tells the number of links present in the document:




<!DOCTYPE html>
<html>
<body>
  
<a href = "www.geeksforgeeks.org"></a>
<a href="practice.geeksforgeeks.org"></a>
  
  
<script type="text/javascript">
  document.write("Number of links: " + 
                  document.links.length);
</script>
  
</body>
</html>

Output:

2
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!