How JavaScript fundamentals saved our checkout: a case study
By Kylo ยท ยท Updated ยท 4 min read

One of the biggest complaints I hear from beginners is that courses don't show how fundamentals are applied "in the wild".
So here's an example of fundamentals saving the day. We were rapidly losing sales because of this bug, and for a while it had me, the other devs on the team and even our CTO completely stumped.
What I love about this example is that it also answers the second biggest complaint: "courses focus on small exercises and not on building things".
Here's the thing: developers don't only build stuff. Most of our time is spent FIXING things and solving problems, often in code we didn't write. That's even more true now that AI writes so much of the first draft. If you want to be a valued member of your team, forget the '10 steps to setting up a React app' and get down in the dirt with JavaScript fundamentals.
The elusive bug
One of our e-commerce landing pages was stopping people from checking out. In fact, none of the buttons on the page worked at all. Not even the JavaScript validation on the checkout form was firing.
In our testing environment, there was no issue. But as soon as the page went live, everything stopped working... ๐ญ

Our first guesses
What's the usual reason a page has no interactivity? No JavaScript.
So first we made sure the JavaScript files were being included in the page. Our next guess was that the JavaScript wasn't running.
Unfortunately, it was! We could even see that the validation had been set up on the checkout form's inputs.
So the problem had to be the event listeners. The buttons all had click listeners, and the form inputs had change listeners, and none of them were firing.
What was overriding these event listeners? ๐ค
Reading third-party code
The only thing left to do was search the source code for the bug. What made this especially tricky was that we hadn't built the website: it was powered by a third-party service. Finding a bug in your own code is bad enough...
Reading code you didn't write is the most important skill any developer has, and watching videos won't improve it. That's why Imagine JavaScript includes so much code for you to read.
The line that brought the entire page down
It all came down to one little line of code...
document.body.innerHTML += customPopup;Let's break up that operation so it's easier to see what's going on... ๐
const customPopup = '<div class="consent-popup">...</div>'; // HTML for the modal
document.body.innerHTML = document.body.innerHTML + customPopup;customPopup was a string of HTML for a modal popup asking customers to consent to advertising.
At first glance, there's nothing wrong with this line. It takes the existing HTML of the page and adds the popup HTML to the end.
But think about it: what value does the expression document.body.innerHTML give back?
A string primitive. In The Great Sync, primitive values are islands.

Here's what you can't do with a primitive like a string:
- mutate it
- add your own properties to it
- attach event listeners or functions to it
A string is just text. Reading innerHTML turns the page into HTML text, and listeners added with addEventListener aren't part of the HTML, so they never make it into the string. (Inline onclick="..." attributes would survive, because they are part of the HTML.)
Then the assignment does the real damage. Setting document.body.innerHTML throws away every element inside the body and builds brand-new ones from that string. The new buttons and inputs look identical, but they're new objects: new ships, in our model. The old ships, the ones with the event listeners attached, are no longer on the page, and any variable still pointing at them is pointing at a ship nobody can see.
In other words, as soon as the code turned the page into a string and back, it stripped away all of the event listeners. Strings can't keep event listeners!
The fix
The safe way to add HTML to a page is to insert only the new part, without rebuilding everything around it:
document.body.insertAdjacentHTML('beforeend', customPopup);insertAdjacentHTML parses just the popup HTML and adds it to the end of the body. The existing elements, and their listeners, are left alone.
Creating strings from objects
The same concept applies when you turn an object into a JSON string:
const book = {
title: 'Learn JS Fundamentals',
getTitle() {
console.log(this.title);
},
};
const jsonString = JSON.stringify(book);After turning book into a JSON string, do you think the getTitle method is still there? Let's find out by turning it back into an object with JSON.parse:
const convertedBack = JSON.parse(jsonString);
console.log(convertedBack);
Nope. As you can see, the function is stripped out: JSON.stringify skips functions, because JSON can't represent them.
Turning objects into strings, whether through innerHTML or JSON.stringify, can keep their structure in text form. But it won't keep things like event listeners and functions, and turning that text back into objects builds brand-new ones containing only what the text described.
Because that's how primitives work: a string can describe an object, but it can't hold one.