A book review app is the perfect next project after a to-do list: it has a form, a list, ratings, filtering, and data that persists. Build it once and you have the pattern for every review, rating, or feedback feature you will ever write.
The data model is one array of review objects, each with a title, author, rating, and review text:
let reviews = JSON.parse(localStorage.getItem("book-reviews")) || [];
function saveReviews() {
localStorage.setItem("book-reviews", JSON.stringify(reviews));
}
The form collects the review. A star rating is five buttons that set a number — simpler and more reliable than a select dropdown:
<form id="review-form">
<input id="title" placeholder="Book title" required>
<input id="author" placeholder="Author" required>
<div id="stars">
<button type="button" data-rating="1">★</button>
<button type="button" data-rating="2">★</button>
<button type="button" data-rating="3">★</button>
<button type="button" data-rating="4">★</button>
<button type="button" data-rating="5">★</button>
</div>
<textarea id="review" placeholder="What did you think?" required></textarea>
<button type="submit">Save review</button>
</form>
let selectedRating = 0;
document.querySelectorAll("#stars button").forEach((button) => {
button.addEventListener("click", () => {
selectedRating = Number(button.dataset.rating);
document.querySelectorAll("#stars button").forEach((b, i) => {
b.classList.toggle("active", i < selectedRating);
});
});
});
document.getElementById("review-form").addEventListener("submit", (e) => {
e.preventDefault();
reviews.unshift({
title: document.getElementById("title").value,
author: document.getElementById("author").value,
rating: selectedRating || 5,
review: document.getElementById("review").value,
date: new Date().toLocaleDateString(),
});
saveReviews();
renderReviews();
e.target.reset();
});
Rendering shows the stars as filled or empty characters, and a filter lets users see only 4-star-and-above reviews:
function stars(rating) {
return "★".repeat(rating) + "☆".repeat(5 - rating);
}
function renderReviews(minRating = 0) {
const list = document.getElementById("review-list");
list.innerHTML = reviews
.filter((r) => r.rating >= minRating)
.map(
(r) => `
<article>
<h3>${r.title}</h3>
<p>by ${r.author} · ${stars(r.rating)} · ${r.date}</p>
<p>${r.review}</p>
</article>`
)
.join("");
}
Need books to review? Here are 10 legal ways to read free eBooks in India — pick a free book, read it, and write your first review in the app you just built.
Two details make the app feel right. First, unshift puts the newest review at the top, so the list reads like a feed. Second, escape user input before injecting it into innerHTML — a review containing <script> should display as text, not run as code. A tiny escape function is enough for a beginner project; a real app would use textContent or a templating library.
Ways to extend it: add an average rating per book, let users edit or delete their reviews, add a search box that filters by title or author, or fetch book covers from the Open Library Covers API using the title. The form-plus-list-plus-filter pattern is the same one behind product reviews, comments, and feedback boards.
Top comments (0)