Are you tired of forgetting tasks and deadlines? It's time to build a practical TODO app using JavaScript to keep track of all your tasks effortlessly. In this step-by-step tutorial, we'll guide you through creating a full-fledged TODO app from scratch, ensuring you learn essential JavaScript concepts along the way.

Before we dive in, make sure you have a basic understanding of HTML and CSS, and that you've set up your development environment with Node.js and a code editor like Visual Studio Code. Let's start by setting up our project and discussing the technologies we'll be using.

Project Setup and Technologies
First, create a new folder for your project and navigate into it:

mkdir todo-app && cd todo-app
Initialize a new npm project and install necessary dependencies:
npm init -y
npm install express ejs
Next, create the following folders and files inside your project folder:

- views - contains our EJS ( Embedded JavaScript templates) files.
- public - holds our static files like CSS and JavaScript.
- app.js - the main application file where we'll write our JavaScript code.
Express Server Setup
Let's set up a basic Express server in app.js. We'll also configure it to serve our static files and EJS templates.

Here's a simple Express server setup:
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Creating the Homepage
Create an index.ejs file inside the views folder. We'll make this our homepage, displaying an input field and a submit button for adding new tasks.

In our app.js file, we'll also create a route for the homepage: /.
Building the TODO Functionality




















Now, let's add the core TODO functionality. We'll use an array to store our tasks, as we're not connecting to a database in this tutorial.
Adding New Tasks
When a user submits a new task, we'll add it to our tasks array and redirect them back to the homepage.
First, create an empty array named tasks in our app.js file. Then, add a new route to handle the post request when a user submits a task:
app.post('/add-task', (req, res) => {
const newTask = req.body.task;
tasks.push(newTask);
res.redirect('/');
});
Displaying Existing Tasks
To display our existing tasks, we'll loop through the tasks array in our index.ejs file and display each task in an unordered list.
Here's how you can do that:
<% tasks.forEach(function(task) { %>
<%= task %>
<% }); %>
Adding Interactivity with JavaScript
Finally, let's add some interactivity to our app by allowing users to mark tasks as completed and delete them.
Marking a Task as Completed
To mark a task as completed, we'll add a checkbox input next to each task. When a user checks the box, we'll remove that task from our array.
Here's how to implement this in our index.ejs file:
<% tasks.forEach(function(task, index) { %>
<% }); %>
In our public folder, create a new file named script.js and add the following JavaScript code to handle the task removal:
function removeTask(index) {
tasks.splice(index, 1);
location.reload();
}
Deleting a Task
To delete a task, we'll add a delete button next to each task. When a user clicks the button, we'll remove that task from our array.
Here's how to implement this in our index.ejs file:
<% tasks.forEach(function(task, index) { %>
<% }); %>
Add the following JavaScript code to our script.js file to handle task deletion:
function deleteTask(task) {
const index = tasks.indexOf(task);
if (index >= 0) {
tasks.splice(index, 1);
location.reload();
}
}
You now have a fully functional TODO app! You can build upon this foundation to add more features like user authentication, task categorization, and more.
Programming should be fun and rewarding. Experiment with this project, and don't hesitate to add your unique twists to make it your own. Happy coding!