learn2kode.in

What Are JavaScript Web APIs?

JavaScript Web APIs are built-in browser features that allow JavaScript to interact with the browser, system hardware, and external services. They are not part of core JavaScript, but are provided by the browser environment to extend JavaScript’s capabilities.

Why JavaScript Web APIs Are Important

Web APIs allow you to:

Without Web APIs, JavaScript would be limited to basic logic only.

How JavaScript Web APIs Work

This is why Web APIs often work asynchronously.

Common JavaScript Web APIs (Beginner-Friendly)

Fetch API

The Fetch API is used to make HTTP requests (GET, POST, etc.).

fetch("https://api.example.com/data")
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

Used for:

Local Storage API

Stores data permanently in the browser.

localStorage.setItem("username", "Learn2Kode");
let name = localStorage.getItem("username");
Session Storage API

Stores data for the current session only.

sessionStorage.setItem("token", "12345");
Geolocation API

Used to get the user’s location (with permission).

navigator.geolocation.getCurrentPosition(position => {
  console.log(position.coords.latitude);
  console.log(position.coords.longitude);
});
DOM API (Brief Reminder)

The DOM itself is a Web API that allows JavaScript to manipulate HTML.

document.getElementById("title").textContent = "Hello Web APIs";
Timer APIs

Provided by the browser for delays and intervals.

setTimeout(() => {
  console.log("Executed after 2 seconds");
}, 2000);

JavaScript Web APIs vs Core JavaScript

Feature Core JavaScript Web APIs
Provided by ECMAScript Browser
Examples Variables, loops Fetch, DOM, Storage
Async support Limited Strong

When to Use JavaScript Web APIs

Use Web APIs when you need to:

Best Practices for Using Web APIs