JAVASCRIPT PROJECT 14

Weather App

Learn how to build a real-time weather application using JavaScript and a weather API.

About This Project

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.

01

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.

02

HTML Structure

Create the basic page structure with a city input, search button, weather result section and areas for temperature, weather condition, humidity and wind speed.

03

City Input

Create an input field where the user can enter a city name such as Delhi, Mumbai, London or New York.

04

Weather API

A weather API provides weather information through an HTTP request. The application sends a city or location request and receives weather data.

05

API Key

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.

06

Fetch API

Use the JavaScript fetch() function to send a request to the weather API and receive the response asynchronously.

07

Async and Await

Use async functions and await to make API code easier to read. Await pauses the function until the requested response is available.

08

JSON Response

Most weather APIs return JSON data. Use response.json() to convert the response into a JavaScript object.

09

Reading Weather Data

Read values such as temperature, weather description, humidity, wind speed and location from the API response.

10

Displaying Weather

Use DOM methods such as textContent to display the API data inside the weather result section.

11

Loading State

Display a loading message while the application is waiting for the API response. This gives the user immediate feedback.

12

Error Handling

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.

13

Temperature Conversion

Weather APIs may provide temperatures in different units. Learn how to display Celsius or convert between Celsius and Fahrenheit when required.

14

Search on Enter

Add a keyboard event so that the user can press Enter inside the city input instead of clicking the search button.

15

Project Flow

The complete flow is: Enter City → Validate Input → Send API Request → Wait for Response → Convert JSON → Read Weather Data → Display Result → Handle Errors.

16

Complete Project

After understanding each step, open the complete Weather App and connect it to a supported weather API using your own API key.

Basic Fetch API Example

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);
}

Handling Errors

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
}

Project Flow

Enter City → Validate Input → Send API Request → Wait for Response → Convert JSON → Read Weather Data → Display Weather → Handle Errors

Important: The final project requires a weather API. Use your own API key from the weather service you choose. Do not publish a private API key in a public website or GitHub repository.
Run Complete Project → ← Back to Projects