How to Create a Simple Todo List in JavaScript?

Estimated read time 1 min read

Here’s a simple example of how you could create a to-do list in JavaScript:

// Create an array to store the to-do items
let toDoList = [];

// Add a new item to the to-do list
function addToDo(task) {
  toDoList.push(task);
}

// Remove a completed item from the to-do list
function completeToDo(index) {
  toDoList.splice(index, 1);
}

// Display the to-do list
function displayToDoList() {
  console.log("My To-Do List:");
  toDoList.forEach((task, index) => {
    console.log(`${index + 1}: ${task}`);
  });
}

// Example usage:
addToDo("Take out the trash");
addToDo("Do the dishes");
addToDo("Vacuum the carpet");

displayToDoList();
// Output:
// My To-Do List:
// 1: Take out the trash
// 2: Do the dishes
// 3: Vacuum the carpet

completeToDo(1);

displayToDoList();
// Output:
// My To-Do List:
// 1: Take out the trash
// 2: Vacuum the carpet

This example demonstrates the basic steps to create a to-do list:

  1. Create an array to store the to-do items.
  2. Write functions to add and remove items from the to-do list.
  3. Write a function to display the to-do list.
  4. Call the functions to add items to the to-do list and display the list.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply