JAVASCRIPT PROJECT 13

Expense Tracker

Learn how to build a JavaScript Expense Tracker with income, expenses, balance and Local Storage.

About This Project

In this project, you will learn how to create an Expense Tracker using HTML, CSS and JavaScript. You will learn arrays, objects, DOM manipulation, localStorage and calculations.

01

Project Introduction

The Expense Tracker is a JavaScript project that allows users to record their income and expenses, calculate the current balance, and manage transaction records.

02

HTML Structure

Create the basic page structure with a title, balance section, transaction form, amount input, transaction type and transaction list.

03

Transaction Form

The form collects important information such as transaction description, amount and whether the transaction is income or expense.

04

Getting Form Values

Use document.getElementById() to select form elements and read the values entered by the user.

05

Transaction Array

Store transaction objects inside a JavaScript array. Each transaction can contain an id, description, amount and type.

06

Adding Transactions

Create a function that validates the input and adds a new transaction object to the transaction array.

07

Income and Expense

Use the transaction type to determine whether an amount should be added to income or counted as an expense.

08

Calculating Balance

Calculate total income and total expenses, then calculate the balance using the formula: Balance = Income - Expenses.

09

Displaying Transactions

Use JavaScript DOM methods to dynamically create transaction items and display them on the webpage.

10

Deleting Transactions

Give every transaction a unique id and create a delete function that removes the selected transaction from the array.

11

Local Storage

Use localStorage to save transactions in the browser so that the user's records remain available after refreshing the page.

12

Loading Saved Data

When the page loads, retrieve saved transaction data from localStorage and convert the JSON data back into a JavaScript array.

13

Updating the Summary

Whenever a transaction is added or deleted, recalculate income, expenses and balance and update the summary section.

14

Reset Data

Create a function that clears all transactions and removes the saved data from localStorage.

15

Project Flow

The complete project flow is: Enter transaction → Validate input → Save transaction → Update array → Calculate income and expense → Update balance → Display transactions → Save to localStorage.

16

Complete Project

After understanding each step, open the complete Expense Tracker project and test different income and expense transactions.

Basic Expense Calculation

const income = transactions
    .filter(item => item.type === "income")
    .reduce((sum, item) => sum + item.amount, 0);

const expenses = transactions
    .filter(item => item.type === "expense")
    .reduce((sum, item) => sum + item.amount, 0);

const balance = income - expenses;

console.log("Income:", income);
console.log("Expenses:", expenses);
console.log("Balance:", balance);

Local Storage Example

localStorage.setItem(
    "aifa_expenses",
    JSON.stringify(transactions)
);

const savedData =
    localStorage.getItem("aifa_expenses");

const transactions =
    savedData ? JSON.parse(savedData) : [];

Project Flow

Enter Transaction → Validate Input → Save Transaction → Update Array → Calculate Income & Expenses → Calculate Balance → Display Transactions → Save to Local Storage

Run Complete Project → ← Back to Projects