Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Web APIs Intro

A Web API is a developer’s ideal tool:

  • It can enhance the browser’s functionality.
  • It can simplify complex tasks.
  • It provides a straightforward syntax for complex code.

What is Web API?

API stands for Application Programming Interface.

A Web API is an interface for web applications.

A Browser API enhances the functionality of a web browser.

A Server API expands the capabilities of a web server.

Browser APIs

All browsers come with a collection of built-in Web APIs to facilitate complex tasks and access data.

 

For instance, the Geolocation API can provide the coordinates of the browser’s current location.

Example

Retrieve the latitude and longitude of the user’s location.

const myElement = document.getElementById(“demo”);

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    myElement.innerHTML = “Geolocation is not supported by this browser.”;
  }
}

function showPosition(position) {
  myElement.innerHTML = “Latitude: “ + position.coords.latitude +
  “<br>Longitude: “ + position.coords.longitude;
}

Third Party APIs

Third-party APIs are not integrated into your browser.

To use these APIs, you must download the code from the web.

Examples include:

  • YouTube API: Enables embedding videos on a website.
  • Twitter API: Allows displaying tweets on a website.
  • Facebook API: Lets you show Facebook information on a website.