A weather dashboard is the perfect second or third JavaScript project: it teaches API calls, async code, and DOM updates, and you end up with something you actually use. This guide builds one with the OpenWeatherMap API, which has a free tier.
First, get a free API key from OpenWeatherMap and keep it in a variable. The core of the app is one fetch call:
const API_KEY = 'YOUR_API_KEY';
async function getWeather(city) {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API_KEY}&units=metric`;
const res = await fetch(url);
if (!res.ok) throw new Error('City not found');
const data = await res.json();
return {
city: data.name,
temp: Math.round(data.main.temp),
feelsLike: Math.round(data.main.feels_like),
humidity: data.main.humidity,
wind: data.wind.speed,
description: data.weather[0].description,
icon: data.weather[0].icon,
};
}
Then render the result into the page and add a 5-day forecast using the /forecast endpoint, which returns data in 3-hour steps — group it by day and show the midday entry for each day.
Three features make this project feel complete:
-
Search history. Save the last five searched cities in
localStorageand render them as clickable chips. -
Geolocation. Use
navigator.geolocationto load the weather for the user's current location on first visit. - Unit toggle. A C/F switch that re-renders the temperatures without a new API call — just convert the numbers you already have.
Handle errors properly: a wrong city name should show "City not found, try again" instead of a blank page. And never commit your API key to GitHub — put it in a .env file or a config file that is gitignored.
This project teaches fetch, async/await, template literals, localStorage, and error handling in one small app. Once it works, try adding an hourly forecast chart or a background that changes with the weather condition.
Top comments (0)