JavaScript Console.Log()
Table of Contents |
---|
SEO links |
console.log() |
GitHub Gists |
GitHub Main Page |
How to use console.log() in most browsers
- treesinfo.netlify.app
- open a browser
- right click > click inspect
- look for console and click that
type console.log('Hello, World!') or code below


count how many links on a page
const links = document.querySelectorAll('a');
const numberOfLinks = links.length;
console.log(`There are ${numberOfLinks} links on this page.`);
There are 486 links on this page.
get document title for the current page
const title = document.title;
console.log(`Page title: ${title}`);
Page title: How to study
Meta description
const metaDescription = document.querySelector('meta[name="description"]').content;
console.log(`Meta description: ${metaDescription}`);
Meta description: Dr. Vini Gora is a professional vocalist. She is a commercial contemporary singer based in Los Angeles. Her music is a beautiful blend of Classical & contemporary sounds.
canonical link
const canonicalLink = document.querySelector('link[rel="canonical"]').href;
console.log(`Canonical link: ${canonicalLink}`);
Canonical link: https://kironroy.dev/
get first and last paragraph
const paragraphs = document.querySelectorAll('p');
console.log(`First paragraph: ${paragraphs[0].textContent}`);
console.log(`Last paragraph: ${paragraphs[paragraphs.length - 1].textContent}`);
First paragraph: Dr. Vini Gora, a New Delhi-born vocalist now residing in Los Angeles, is a classically trained singer with a PhD in Hindustani classical music. She is a disciple of Pt. Balmiki Sharma [disciple of Ustad Ghulam Hussain Khan of Rampur Sehaswan Gharana and Pt. Kundan Lal Sharma Bhoot of Patiala Gharana]. Last paragraph: click/tap to call
Last paragraph: click/tap to call
Basic page structure
const sections = document.querySelectorAll('section');
const headers = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
const paragraphs = document.querySelectorAll('p');
console.log(`Total number of sections: ${sections.length}`);
console.log(`Total number of headers: ${headers.length}`);
console.log(`Total number of paragraphs: ${paragraphs.length}`);
Total number of sections: 1
Total number of headers: 46
Total number of paragraphs: 23
get images and alternate text
const images = document.querySelectorAll('img');
images.forEach((image, index) => {
console.log(`Image ${index + 1} alt text: ${image.alt}`);
});
Image 1 alt text: Fish
Image 2 alt text: Aquariums & Tanks
Image 3 alt text: Pumps & Flow
Image 4 alt text: Filtration & Skimmers
Image 5 alt text: Lighting
const structuredData = document.querySelectorAll('script[type="application/ld+json"]');
structuredData.forEach(data => console.log(`Structured data: ${data.textContent}`));
get structure data in JSON format
Structured data:
{
"@context": "https://schema.org",
"@type": "Organization",
"name": "The Co",
"url": "https://co.com/",
"logo": "https://co.com/wp-content/uploads/2023/02/White-Text-Co.png",
"contactPoint": {
"@type": "ContactPoint",
"telephone": "562-555-5555",
"contactType": "technical support",
"contactOption": "TollFree",
"areaServed": "US",
"availableLanguage": "en"
},
"sameAs": [
"https://www.facebook.com/The-Co-104347378904055",
"https://www.instagram.com/co/"
]