JavaScript Project - Weather App

In this tutorial, we will learn how to create a simple Weather App in JavaScript.

JavaScript Weather Application

Create a folder called weather-app as project workspace and we will create all the project files inside this folder.

1. index.html

Let's create index.html and add the following code to it:

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Weather App</title>
        <link rel="stylesheet" href="style.css" />
        <script src="script.js" defer></script>
        <form id="form">
                placeholder="Search by location"
        <main id="main"></main>

2. script.js

Let's create a JavaScript file named script.js and add the following JavaScript code to it:

const apikey = "3265874a2c77ae4a04bb96236a642d2f";

const main = document.getElementById("main");
const form = document.getElementById("form");
const search = document.getElementById("search");

const url = (city) =>

async function getWeatherByLocation(city) {
    const resp = await fetch(url(city), { origin: "cors" });
    const respData = await resp.json();



function addWeatherToPage(data) {
    const temp = KtoC(data.main.temp);

    const weather = document.createElement("div");

    weather.innerHTML = `
        <h2><img src="${[0].icon}@2x.png" /> ${temp}°C <img src="${[0].icon}@2x.png" /></h2>

    // cleanup
    main.innerHTML = "";


function KtoC(K) {
    return Math.floor(K - 273.15);

form.addEventListener("submit", (e) => {

    const city = search.value;

    if (city) {

3. style.css

Let's create a CSS file named style.css and add the following CSS code to it:

@import url(";400;600&display=swap");

* {
    box-sizing: border-box;

body {
    background: linear-gradient(300deg, #ced1d6, #bfc0c0);
    font-family: "Poppins", sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: 0;
    min-height: 100vh;

input {
    background-color: #fff;
    border: none;
    border-radius: 25px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    font-family: inherit;
    font-size: 1rem;
    padding: 1rem;
    min-width: 300px;

input:focus {
    outline: none;

.weather {
    font-size: 2rem;
    text-align: center;

.weather h2 {
    display: flex;
    align-items: center;
    margin-bottom: 0;

Open index.html in Browser

Let's open the index.html file in the browser, and you will be able to see the following screen:

More Free JavaScript Projects with Source Code