A stock price tracker is a great JavaScript project because it combines everything beginners need to learn: fetching data from an API, rendering a list, updating it on a timer, and handling errors gracefully. In this tutorial we will build a tracker that watches a few stocks, shows their current price and daily change, and refreshes automatically every minute.
First, the data source. For learning, use a free public API or a mock data file — the important part is the shape of the data, not the provider. Each stock has a symbol, a name, a price, and a previous close. From those two numbers we compute the change and the percentage change.
// Fetch the latest prices (replace with your API of choice)
async function fetchPrices(symbols) {
const res = await fetch(
`https://api.example.com/quotes?symbols=${symbols.join(",")}`
);
if (!res.ok) throw new Error("Failed to fetch prices");
return res.json(); // [{ symbol, name, price, prevClose }, ...]
}
function changePercent(stock) {
const change = stock.price - stock.prevClose;
const percent = (change / stock.prevClose) * 100;
return { change, percent };
}
Next, the rendering. Each stock gets a row showing its symbol, name, price, and change — green for up, red for down. A setInterval refreshes the data every 60 seconds, and a "last updated" timestamp tells the user how fresh the numbers are. If a fetch fails, show the last good data with a small warning instead of a blank screen.
async function refresh() {
try {
const stocks = await fetchPrices(["RELIANCE", "TCS", "INFY"]);
render(stocks);
document.getElementById("updated").textContent =
"Last updated: " + new Date().toLocaleTimeString();
} catch (err) {
document.getElementById("error").textContent =
"Could not refresh prices — showing last known data.";
}
}
setInterval(refresh, 60_000);
refresh();
A tracker is also a reminder that watching prices is only half of investing. Before putting real money into the market, it helps to understand how it works — and a good starting point is reading about the best stock market books for beginners, which explain the basics in simple language before you invest a single rupee.
To go further, add a watchlist form so users can add their own symbols, sort the list by biggest gainers and losers, and store the watchlist in localStorage. Add a simple line chart with a canvas element to show the day's movement. Each feature is small on its own, and together they make a genuinely useful tool.
This project teaches the core skills of every data-driven app: async fetching, error handling, timers, and rendering from state. Build it in an afternoon, and you will be ready for bigger API projects.
Top comments (0)