Build a Real-Time Weather App using JavaScript 🌦️

🌦️ Build a Real-Time Weather App using JavaScript

Hello everyone πŸ‘‹
Welcome back!

In this project, we will build a Real-Time Weather App using HTML, CSS & JavaScript 🌦️

This project is perfect for beginners who want to learn:

- API Integration
- Fetch API
- Async/Await
- DOM Manipulation
- Real-world JavaScript Projects

---

🌟 Features of Weather App

✔ Search weather by city
✔ Real-time weather data
✔ Temperature in °C
✔ Weather conditions
✔ Responsive design
✔ Beginner-friendly project

---

🧱 Step 1: HTML Code

HTML

<!DOCTYPE html>
<html>
<head>
    <title>Weather App</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

<div class="container">
    <h1>Weather App</h1>

    <div class="search-box">
        <input type="text" id="city" placeholder="Enter city name">
        <button onclick="getWeather()">Search</button>
    </div>

    <div id="result"></div>
</div>

<script src="script.js"></script>
</body>
</html>

---

🎨 Step 2: CSS Code

CSS

*{
    margin:0;
    padding:0;
    box-sizing:border-box;
    font-family:Arial;
}

body{
    height:100vh;
    display:flex;
    justify-content:center;
    align-items:center;
    background:linear-gradient(135deg,#4facfe,#00f2fe);
}

.container{
    background:white;
    padding:30px;
    width:400px;
    border-radius:15px;
    text-align:center;
    box-shadow:0 10px 25px rgba(0,0,0,0.2);
}

h1{
    margin-bottom:20px;
}

.search-box{
    display:flex;
    gap:10px;
}

input{
    flex:1;
    padding:10px;
    border:2px solid #ddd;
    border-radius:8px;
}

button{
    padding:10px 15px;
    background:#007bff;
    color:white;
    border:none;
    border-radius:8px;
    cursor:pointer;
}

#result{
    margin-top:20px;
}

---

⚡ Step 3: JavaScript Code

JavaScript

async function getWeather(){

    const city = document.getElementById("city").value;

    const apiKey = "YOUR_API_KEY";

    const url =
    `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;

    try{

        const response = await fetch(url);
        const data = await response.json();

        if(data.cod === "404"){
            document.getElementById("result").innerHTML =
            "<p style='color:red;'>City not found!</p>";
            return;
        }

        document.getElementById("result").innerHTML = `
            <h2>${data.name}</h2>
            <h3>${Math.round(data.main.temp)}°C</h3>
            <p>${data.weather[0].description}</p>
            <p>Humidity: ${data.main.humidity}%</p>
            <p>Wind Speed: ${data.wind.speed} km/h</p>
        `;

    }catch(error){

        document.getElementById("result").innerHTML =
        "<p>Something went wrong!</p>";
    }
}

---

πŸ”‘ Get Free API Key

πŸ‘‰ Create a free account on
"openweathermap.org" (https://reference-url-citation.invalid/0)

Then generate your free API key.

Replace:

JavaScript

YOUR_API_KEY

with your actual API key.

---

🎯 What You Will Learn

✅ API Integration
✅ Fetch Data using JavaScript
✅ Async/Await
✅ DOM Manipulation
✅ Real-world Project Development

---

⭐ Tips to Improve Project

πŸ‘‰ Add weather icons ☀️
πŸ‘‰ Add dark mode πŸŒ™
πŸ‘‰ Show more details like humidity & wind speed
πŸ‘‰ Add loading animation ⚡

---

πŸ“Œ Conclusion

This was a Real-Time Weather App Project using JavaScript 🌦️

A perfect beginner project to improve your JavaScript skills and build real-world applications πŸš€

---

πŸ“’ Also Read

πŸ‘‰ Calculator Project
πŸ‘‰ To-Do App Project
πŸ‘‰ JavaScript Interview Guide

---

πŸ‘‰ Save and share this project πŸ“Œ
πŸ‘‰ Follow for more coding content πŸ’»

Comments

Popular posts from this blog

Build a Simple Calculator using HTML, CSS & JavaScript (Beginner Project)

Top 10 Python Programs for Beginners (Easy + Practical Guide)

JavaScript Notes for Beginners (Simple + Easy Guide) ⚡