Project Introduction
The Weather App is a JavaScript project that uses an online weather API to retrieve current weather information for a city and display it dynamically on a webpage.
Learn how to build a real-time weather application using JavaScript and a weather API.
In this project, you will learn how JavaScript can communicate with an external API. The application will search for a city and display its current weather information dynamically.
The Weather App is a JavaScript project that uses an online weather API to retrieve current weather information for a city and display it dynamically on a webpage.
Create the basic page structure with a city input, search button, weather result section and areas for temperature, weather condition, humidity and wind speed.
Create an input field where the user can enter a city name such as Delhi, Mumbai, London or New York.
A weather API provides weather information through an HTTP request. The application sends a city or location request and receives weather data.
Many weather services require an API key. The key is used to authenticate requests. Never expose a private API key in a public production application.
Use the JavaScript fetch() function to send a request to the weather API and receive the response asynchronously.
Use async functions and await to make API code easier to read. Await pauses the function until the requested response is available.
Most weather APIs return JSON data. Use response.json() to convert the response into a JavaScript object.
Read values such as temperature, weather description, humidity, wind speed and location from the API response.
Use DOM methods such as textContent to display the API data inside the weather result section.
Display a loading message while the application is waiting for the API response. This gives the user immediate feedback.
Use try and catch to handle network errors. Also check the API response status and show a useful message when a city cannot be found.
Weather APIs may provide temperatures in different units. Learn how to display Celsius or convert between Celsius and Fahrenheit when required.
Add a keyboard event so that the user can press Enter inside the city input instead of clicking the search button.
The complete flow is: Enter City → Validate Input → Send API Request → Wait for Response → Convert JSON → Read Weather Data → Display Result → Handle Errors.
After understanding each step, open the complete Weather App and connect it to a supported weather API using your own API key.
async function getWeather(city) {
const response = await fetch(
`YOUR_API_URL`
);
if (!response.ok) {
throw new Error("Weather data not found");
}
const data = await response.json();
console.log(data);
}
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error("Unable to get weather data");
}
const data = await response.json();
} catch (error) {
console.error(error);
// Show an error message to the user
}
Enter City → Validate Input → Send API Request → Wait for Response → Convert JSON → Read Weather Data → Display Weather → Handle Errors