DEV Community

Cover image for Vanilla JavaScript Is Teaching Me More Than I Expected: What building a Todo-list application taught me about structuring JavaScript applications.
Deolawage
Deolawage

Posted on Edited on

Vanilla JavaScript Is Teaching Me More Than I Expected: What building a Todo-list application taught me about structuring JavaScript applications.

Hello, World!

For a while, I've been learning JavaScript through The Odin Project.

I've worked through variables, functions, arrays, objects, DOM manipulation, classes, and several other concepts. Some were easier to understand than others. Some took considerably longer than I expected.

But there's something I've started noticing as I've moved from individual exercises to building projects.

Vanilla JavaScript is teaching me more than I expected.

Not necessarily because JavaScript itself is complicated, but because building an actual application forces you to think about things you might not consider when solving smaller exercises.

I experienced this while building my Todo-list application.

It started as a simple Todo-list

The idea wasn't particularly complicated.

I wanted users to be able to create projects, add todos to those projects, mark todos as completed, edit them, and delete them.

I also wanted the application to remember everything after the browser was closed.

Simple enough, right?

Well, the more I thought about the requirements, the more I realised that I needed to consider how the different parts of the application would work together.

For example:

  • How should todos be organised?
  • How should projects manage their todos?
  • What happens when a project is deleted?
  • What happens when the currently selected project is deleted?
  • How do I preserve application data after a page reload?
  • How should the application restore that data?

These weren't questions I could answer by simply writing a few event listeners.

I needed to think about the structure of the application.

Learning to organise my JavaScript

One of the things I wanted to practise was writing JavaScript that wasn't all squeezed into one file.

I started separating the application into different modules and responsibilities.

I created separate classes for Todo, Project, and ProjectManager.

The idea was to give each one a responsibility.

A Todo would represent an individual task and its properties. A Project would manage a collection of todos, while the ProjectManager would handle the collection of projects and the overall project-related operations.

I also introduced an AppController to coordinate the application.

For the DOM, I separated the interface-related functionality from the application logic.

This was an interesting experience because I had to think about where different operations belonged.

Should this method be inside the Todo class?

Should the ProjectManager handle this operation?

Should the DOM module know anything about how the application stores its data?

I didn't always have immediate answers.

Sometimes, I had to stop and reconsider how I had structured something.

But that was part of the learning process.

I was beginning to understand that writing JavaScript wasn't just about making something work. It was also about making decisions concerning how the code should be organised.

LocalStorage introduced another challenge

I wanted my Todo-list application to preserve its data, so I decided to use LocalStorage.

At first, the idea seemed straightforward: save the application data and retrieve it when the page loads.

But I soon realised there was more to it.

LocalStorage stores data as strings. This meant I needed to convert my application data into JSON before saving it and convert it back into JavaScript data when retrieving it.

That's where things got interesting.

I was using JavaScript classes to structure my application. My Todo and Project objects weren't just collections of properties; they also had methods responsible for certain behaviours.

For example, a Todo had a toggleComplete() method for changing its completion status.

But when I converted an instance of a class into JSON, something important happened.

JSON preserved the object's data, but it didn't preserve its class methods.

When I retrieved that data using JSON.parse(), I got ordinary JavaScript objects.

They contained the properties I had saved, but they weren't automatically restored as instances of their original classes.

This meant I couldn't simply retrieve a saved Todo and expect its methods to work.

I had to think about reconstruction.

My solution was to recreate the class instances from the retrieved data.

Instead of treating the objects returned by JSON.parse() as complete application objects, I needed to pass their properties back into the appropriate constructors.

The same idea applied to Projects, which also contained collections of todos.

I had to make sure that when a project was reconstructed, its todos were reconstructed too.

This was an important distinction for me.

I began to understand that saving an object's data and preserving the object's behaviour are two different things.

There were other details to consider as well.

I needed to preserve the IDs of my todos and projects instead of generating new ones whenever the application loaded. I also needed to preserve information such as completion status and the currently selected project.

Otherwise, the application might restore the data but behave differently from how it did before the page was closed.

This made me think more carefully about what exactly needed to be saved and how it should be restored.

I eventually separated this responsibility into a storage module.

Looking back, LocalStorage introduced one of the more interesting challenges in the project because it forced me to understand the relationship between JavaScript objects, JSON, and class instances more deeply.

It wasn't just about storing information anymore.

It was about bringing the application back to a state where everything could work as expected.

Debugging is part of building

Not everything worked smoothly.

I encountered issues with my project structure, including accidentally having nested src folders. This caused problems with my Webpack entry and build process.

After fixing that, I encountered another issue involving a selected project returning null.

There were moments when I had to go back through my code, trace what was happening, and figure out where things were going wrong.

One thing I began appreciating was the importance of understanding the flow of data through an application.

When something doesn't work, changing random lines of code isn't always the answer.

Sometimes, you need to understand what information is being passed, where it is coming from, and what the receiving part of your application expects.

I still have a lot to learn about debugging, but projects like this are giving me opportunities to practise.

Webpack became part of the experience

I also used Webpack to bundle the application.

I had to work with ES modules, configure the development environment, and separate my Webpack configuration into common, development, and production configurations.

This introduced another layer of learning.

I wasn't just thinking about the JavaScript application anymore. I also had to understand how the files were being processed and bundled.

There were configuration issues along the way, but gradually, things started making more sense.

I began to see how tools like Webpack fit into the development process rather than treating them as something I needed to configure just to make a project run.

So, what did vanilla JavaScript teach me?

Looking back at the project, I think the biggest lesson was that building an application requires a different kind of thinking.

When you're learning individual JavaScript concepts, you can focus on understanding one thing at a time.

But when building an application, those concepts start interacting.

Classes interact with modules. Modules communicate with one another. The application manages state. The DOM reflects that state. LocalStorage preserves data between sessions.

And suddenly, you're no longer just thinking about how to write a function.

You're thinking about how the entire application should behave.

That's what I found interesting about building my Todo-list application.

I had to make decisions, reconsider approaches, debug problems, and connect different concepts I had previously learned separately.

I also began appreciating why developers spend time thinking about application architecture, even for projects that might appear simple.

I'm not in a hurry to leave vanilla JavaScript

I know there are frameworks and libraries that can make building certain applications easier.

React is something I intend to learn, and I'm looking forward to exploring it.

But I don't want to rush through vanilla JavaScript just because there are other tools available.

I want to understand what happens underneath the abstractions.

I want to understand how the DOM works, how application state is managed, how data flows through an application, and how different parts of a codebase communicate.

I believe that understanding these fundamentals will make learning other tools more meaningful.

My Todo-list application wasn't the most complicated application in the world, but it challenged me in ways I didn't anticipate.

And that's something I appreciate.

I'm still learning. I'm still making mistakes. And I'm still figuring things out.

But I'm beginning to understand that sometimes, the best way to learn a programming language is to stop treating it as a collection of lessons and start using it to build something.

Vanilla JavaScript is teaching me more than I expected. And I think I'm only getting started.

Back to the code. 🫡.

If you're interested in seeing how I approached the project, you can explore my implementation on GitHub:https://github.com/Deolawage/Todo-app

Top comments (0)