Nearby lessons

20 of 22

JavaScript: fetch() API

📌 What is Fetch API?

The fetch() API is used to make HTTP requests in JavaScript. It returns a Promise that resolves to the response of the request. It's commonly used to get data from APIs or load resources like text or JSON.

1️⃣ Fetch Text File using then()

Fetch a local file (like readme.txt) and read its content using response.text().

Code Example
PREVIEW READY
Loading Editor...
Live Preview

2️⃣ Short Version using Arrow Functions

A more concise version using arrow functions and implicit returns.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

3️⃣ Fetch JSON Data from API

Use fetch() to get JSON data from a REST API and log it in the console.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

4️⃣ Display API Data on the Page

This example fetches user data from an API and displays each user's name in the browser.

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧠 Test Your Knowledge

3 Questions

Progress: 0 / 3
Keep Going!AJS - async and await