Logo

dev-resources.site

for different kinds of informations.

How to Use JavaScript to Reduce HTML Code: A Simple Example

Published at
1/15/2025
Categories
webdev
javascript
html
tutorial
Author
egroene
Categories
4 categories in total
webdev
open
javascript
open
html
open
tutorial
open
Author
7 person written this
egroene
open
How to Use JavaScript to Reduce HTML Code: A Simple Example

A huge part of being a good programmer is keeping your code as easy-to-debug as possible, as well as writing code that is easy to scale without making mistakes. If you’re working on, say, a blog website, the amount of content on the site will grow over time, so every time you come back to the project, you want to be able to do so quickly & without making mistakes, which could harm your client’s reputation by making the site look bad.

Luckily for us, God (or rather, Brendan Eich) created JavaScript, which can help make our code more efficient & easier to debug. In this article, I will give a brief example of how I used JS to do exactly this while building a blog website.

On a typical blog website, you should make it easy for visitors to find you on social media, as this is the best way to grow a following online. One way to do this is to display links to your social media channels on every page, maybe in a footer, or in a panel that sits on the side of the screen at all times, as seen in the photo below:

Panel with social links (encircled)

If you want this panel to display on all pages of the site, one way to accomplish this would be to copy & paste several lines (14 in this case) of the same code onto every HTML page in the project. These lines are pictured below:

HTML to create the social media panel, as seen in the first picture in this article

Fourteen lines of code may not seem like much to you, and, in a way, you’re right. It really wouldn’t be that difficult to just copy & paste these onto every new HTML page you create, especially if the project is small. However, it makes the code clunkier, meaning the page won’t load as soon as it would with fewer lines, as well as more difficult to modify or debug. In the photo above, I put hashtags in the href part of the anchor tags, as this is only a demo.

However, in a realistic situation, these would link to a real social media profile’s URL. So, imagine your client changes their Instagram username. This could lead to the URL, that is put into href, being different than it was before. As the developer, you would have to get that new link & paste it into every single HTML page & in the appropriate spot. Not only is this cumbersome, but it could lead to errors if you are not careful, especially if you get bored doing this repetitive task.

A better way to accomplish this would be to use JS. In the picture below, I created functionality in JS to add these links to a div that is already present on all HTML pages, which is stored in the variable socialsPanel.

Functionality to populate the social panel’s HTML

When we use a function like this, we only need to enter each link’s information in one place: inside this function. So, if the information for a particular link changes, we would only need to change it here, instead of on every single HTML page if we hadn’t used JS & had instead used the copy/paste method. If we wanted to add or subtract links, we would only have to do so in this function.

Not only is this a huge timesaver, but, as mentioned before, it saves lines of HTML code, since to populate a page with these links, we wouldn’t need to copy & paste the 13* lines of code into every HTML document, only call the function upon loading the page. Calling this function, popSocialsPanel(), on a page, as you can see underlined in red in the next photo, adds no new line of code (just ignore the other functions there for now):

Lines that contain the series of links in this case, since we kept the div , to which we wanted to add the links, on each HTML document

Call function upon loading of a page where you want the social media panel to display

Let’s take a minute to do the math on all of this. Not including the comments, we added 9 new lines of code in our JS file, which we only have to write once, while subtracting 13 lines of HTML code. So, if our project had only one HTML document, we would still be saving 4 lines of code, which is good, but we would be saving 13 lines of code while implementing this functionality for every HTML document we add to the project, which is great.

On this particular project, I have saved 56 lines of code, so far, by using this method. For every page for a blog post I add, I will be saving another 13.

I hope you found this relatively simple example helpful, but I guess the takeaway from this article would be that you should develop a certain instinct as a programmer; if you feel that you are repeating yourself while writing code, there is probably a more-efficient way to write it.

If you have any comments or questions, I’d be happy to respond. If you found this article helpful, I’d appreciate a nice comment or a clap or two, so I can know what content my readers like. If you think others would find this useful, please share it with them.

Thanks for reading!

Originally published on Medium for JavaScript in Plain English on September 28, 2022

javascript Article's
30 articles in total
JavaScript is a versatile language for web development, enabling interactive and dynamic user experiences across all major browsers.
Favicon
7 Developer Tools That Will Boost Your Workflow in 2025
Favicon
Creating a live HTML, CSS and JS displayer
Favicon
Designing for developers means designing for LLMs too
Favicon
Unique Symbols: How to Use Symbols for Type Safety
Favicon
Flutter vs React Native in 2025: A Comprehensive Comparison
Favicon
How To Build Beautiful Terminal UIs (TUIs) in JavaScript 2: forms!
Favicon
[Boost]
Favicon
Cómo Iniciar y Crecer como Desarrollador Frontend en 2025
Favicon
Building bun-tastic: A Fast, High-Performance Static Site Server (OSS)
Favicon
My React Journey: Project
Favicon
How to Use JavaScript to Reduce HTML Code: A Simple Example
Favicon
Easy Discount Calculation: Tax, Fees & Discount Percentage Explained
Favicon
GraphQL Transforming API Development
Favicon
7 Mistakes Developers Make When Learning a New Framework (and How to Avoid Them)
Favicon
Why Is Everyone Unhappy With JavaScript? | State of Javascript 2024 Survey
Favicon
Decreasing server load by using debouncing/throttle technique in reactjs
Favicon
What is Next Js: A Beginner's guide to Next Js
Favicon
Angular Addicts #33: NgRx 19, using the Page Object Model in tests, Micro Frontends using Vite & more
Favicon
Debugging Adventure Day 1: What to Do When Your Code Doesn’t Work
Favicon
10 Must-Bookmark Open Source Projects for Developers
Favicon
Build a Secure Password Generator with Javascript
Favicon
Everything you need to know about load testing concurrent users
Favicon
forms in the terminal w/ JavaScript? if you love Terminal UI's check this out!
Favicon
340+ Websites every developer should know
Favicon
Learning Vue
Favicon
Test Scenarios vs. Test Cases: Understanding the Differences
Favicon
LeetCode Challenge: 290. Word Pattern - JavaScript Solution 🚀
Favicon
Understanding Node.js Cluster: The Core Concepts
Favicon
Speed Up Your Frontend Development 10x with These Mock Tools 🚀
Favicon
[Boost]

Featured ones: