-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
186: Building a Basic Weather Widget Using an API
How do I actually call the weather API and get the data?
When you're building a widget, you aren't storing the weather data yourself—you're asking a server for it. Most modern APIs use JSON, and the cleanest way to handle this in JavaScript is with async and await. I've found that using .then() chains can get messy quickly, especially when you start nesting calls.
Here is how I usually structure a basic fetch call for something like OpenWeatherMap. Notice how I wrap it in an async function so I can use await to pause the execution until the data actually arrives.
async function getWeather(city) {
const apiKey = 'YOUR_API_KEY_HERE';
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error("Something went wrong with the network request:", error);
}
}
The API returns a huge object—how do I extract just the temperature and city name?
If you console.log the response from a weather API, it looks like a mountain of data. You don't need all of it. To make a widget, you just need to "drill down" into the object. For instance, OpenWeatherMap nests the temperature inside a main object and the weather description inside an array called weather.
I like to destructure these values immediately to keep the code readable. Instead of writing data.main.temp everywhere, do this:
async function updateWidget() {
const data = await getWeather('London');
// Destructuring the nested properties we actually care about
const { name, main: { temp }, weather } = data;
const description = weather[0].description;
document.getElementById('city-name').innerText = name;
document.getElementById('temp-display').innerText = `${Math.round(temp)}°C`;
document.getElementById('status').innerText = description;
}
What's the best way to handle it when a user enters a city that doesn't exist?
This is where a lot of developers trip up. fetch only throws an error (triggering the catch block) if there is a network failure—like your internet going out. If the API returns a 404 Not Found because the user typed "FakeCity123", fetch actually considers that a "successful" response.
You have to manually check if the response was okay. I always check the response.ok property before I even try to parse the JSON. If it's false, I throw an error myself to jump straight to the catch block where I can tell the user what happened.
async function getWeather(city) {
const apiKey = 'YOUR_API_KEY_HERE';
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('City not found. Please try again!');
}
return await response.json();
} catch (error) {
// Now this handles both network crashes AND 404s
document.getElementById('error-message').innerText = error.message;
}
}📋 Practical Task
Build a Dynamic Weather Search Toggle
Create a small weather widget interface. It should include an input field for a city name, a search button, and a display area for the results. Using a free API (like OpenWeatherMap), write a JavaScript function that:
- Triggers the API call only when the search button is clicked.
- Updates the DOM to show the city name, current temperature, and a brief weather description.
- Displays a specific error message in red text if the API returns a 404 (City not found).
- Uses
Math.round()to ensure the temperature is displayed as a whole number.
There are no comments for now.