A shopping cart is the project that separates tutorial learners from builders. It is not one feature — it is state management, money maths, and persistence, all in one small app. Build it once and you understand how every online store works.
The plan
- A product list rendered from an array (name, price, image).
- An "Add to cart" button on each product.
- A cart panel showing items, quantities, and a running total.
- Quantity controls, a remove button, and a total that updates on every change.
The HTML
<div id="products"></div>
<h2>Your Cart</h2>
<div id="cart"></div>
<p>Total: ₹<span id="total">0</span></p>
The JavaScript
const products = [
{ id: 1, name: 'Notebook', price: 199 },
{ id: 2, name: 'Pen Set', price: 149 },
{ id: 3, name: 'Backpack', price: 999 }
];
// Cart is an object: product id -> quantity
let cart = JSON.parse(localStorage.getItem('cart') || '{}');
function save() {
localStorage.setItem('cart', JSON.stringify(cart));
}
function renderProducts() {
document.getElementById('products').innerHTML = products.map(p => `
<div class="product">
<h3>${p.name}</h3>
<p>₹${p.price}</p>
<button data-add="${p.id}">Add to Cart</button>
</div>
`).join('');
}
function renderCart() {
const box = document.getElementById('cart');
const entries = Object.entries(cart);
if (entries.length === 0) {
box.innerHTML = '<p>Your cart is empty.</p>';
} else {
box.innerHTML = entries.map(([id, qty]) => {
const p = products.find(x => x.id === Number(id));
return `<div class="cart-item">
<span>${p.name} × ${qty}</span>
<button data-inc="${id}">+</button>
<button data-dec="${id}">−</button>
<button data-remove="${id}">Remove</button>
</div>`;
}).join('');
}
const total = entries.reduce((sum, [id, qty]) => {
const p = products.find(x => x.id === Number(id));
return sum + p.price * qty;
}, 0);
document.getElementById('total').textContent = total;
}
document.addEventListener('click', (e) => {
const add = e.target.dataset.add;
const inc = e.target.dataset.inc;
const dec = e.target.dataset.dec;
const rem = e.target.dataset.remove;
if (add) cart[add] = (cart[add] || 0) + 1;
if (inc) cart[inc] += 1;
if (dec) {
cart[dec] -= 1;
if (cart[dec] <= 0) delete cart[dec];
}
if (rem) delete cart[rem];
if (add || inc || dec || rem) {
save();
renderCart();
}
});
renderProducts();
renderCart();
Three details beginners miss
- Store the cart as id → quantity, not as a list of items. Adding the same product twice just increments a number. A list of duplicate items makes the total and the remove button far harder to get right.
- Money maths must run on every render. The total is computed from the cart every time, never stored separately. A stored total drifts out of sync the moment you forget to update it once.
-
Delete at zero. When quantity hits zero, remove the key entirely. A
0hanging around in the cart is the bug that makes your empty-cart message never show.
What to add next
A checkout form, a discount code (10% off when the code matches), a "clear cart" button, and syncing the cart to a backend so it follows the user across devices.
Top comments (0)