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().

Example02
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

2️⃣ Short Version using Arrow Functions

A more concise version using arrow functions and implicit returns.

Example03
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

3️⃣ Fetch JSON Data from API

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

Example04
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

4️⃣ Display API Data on the Page

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

Example05
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3