7 More JavaScript Web APIs to Build Futuristic Websites you didn’t Know 🤯

Tapajyoti Bose
4 min readJun 25, 2023

--

Welcome back to the future! This is the second article on futuristic JavaScript Web APIs, so if you haven’t read the first one, you are recommended to do so here

Here are the 7 more cutting-edge JavaScript Web APIs to add an enchanting touch to your projects to make users part with their money 💰

1. Web Speech

The Web Speech API enables you to incorporate voice data into web apps. The Web Speech API has two parts: SpeechSynthesis (Text-to-Speech), and SpeechRecognition (Asynchronous Speech Recognition)

// Speech Synthesis
const synth = window.speechSynthesis;
const utterance = new SpeechSynthesisUtterance("Hello World");
synth.speak(utterance);

// Speech Recognition
const SpeechRecognition =
window.SpeechRecognition ?? window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.start();
recognition.onresult = (event) => {
const speechToText = event.results[0][0].transcript;
console.log(speechToText);
};

NOTES

  1. Even though Speech Synthesis is supported by all major browsers with 96% coverage, Speech Recognition is still a bit early to use in production with only 86% coverage.
  2. The API CANNOT be used without the user interaction (eg: click, keypress, etc).

2. Page Visibility

The Page Visibility API allows you to check if the page is visible to the user or not. This is useful when you want to pause a video.

There are 2 methods to perform this check:

// Method 1
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
document.title = "Visible";
return;
}
document.title = "Not Visible";
});

// Method 2
window.addEventListener("blur", () => {
document.title = "Not Visible";
});
window.addEventListener("focus", () => {
document.title = "Visible";
});

The difference between the two methods is that the second one will be triggered if you switch over to another app OR a different tab, while the first one will be triggered only if you switch over to another tab.

3. Accelerometer

The Accelerometer API allows you to access the acceleration data from the device.

This can be used to create games that use the device’s motion control or add interaction if the user shakes the device, the possibilities are endless!

const acl = new Accelerometer({ frequency: 60 });

acl.addEventListener("reading", () => {
const vector = [acl.x, acl.y, acl.z];
const magnitude = Math.sqrt(vector.reduce((s, v) => s + v * v, 0));
if (magnitude > THRESHOLD) {
console.log("I feel dizzy!");
}
});

acl.start();

You can ask for Accelerometer permission using:

navigator.permissions.query({ name: "accelerometer" }).then((result) => {
if (result.state === "granted") {
// now you can use accelerometer api
}
});

4. Geo-location

The Geolocation API allows you to access the user’s location.

This can be extremely useful if you are building anything related to maps or location-based services.

navigator.geolocation.getCurrentPosition(({ coords }) => {
console.log(coords.latitude, coords.longitude);
});

You can ask for Geolocation permission using:

navigator.permissions.query({ name: "geolocation" }).then((result) => {
if (result.state === "granted") {
// now you can use geolocation api
}
});

5. Web worker

Web Workers makes it possible to run a script operation in a background thread separate from the main execution thread of a web application. The advantage of this is that laborious processing can be performed in a separate thread, allowing the main (usually the UI) thread to run without being blocked/slowed down.

// main.js
const worker = new Worker("worker.js");
worker.onmessage = (e) => console.log(e.data);
worker.postMessage([5, 3]);

// worker.js
onmessage = (e) => {
const [a, b] = e.data;
postMessage(a + b);
};

6. Resize Observer

The Resize Observer API allows you to observe the size of an element and handle the changes with ease.

It is exceptionally useful when you have a resizable sidebar.

const sidebar = document.querySelector(".sidebar");
const observer = new ResizeObserver((entries) => {
const sidebar = entries[0];
//Do something with the element's new dimensions
});
observer.observe(sidebar);

7. Notification

Ah, Notifications! The annoying little popups (or bubbles of dopamine, based on where you lie on the spectrum).

The Notification API, just as the name suggests, allows you to send notifications to annoy users (bundle it with the Page Visibility API to annoy them even more 😈)

Notification.requestPermission().then((permission) => {
if (permission === "granted") {
new Notification("Hi there!", {
body: "Notification body",
icon: "https://tapajyoti-bose.vercel.app/img/logo.png",
});
}
});

Side Note

Some of the APIs mentioned above are still in the experimental stage and are not supported by all browsers. So, if you want to use them in production, you should check if the browser supports them

For example:

if ("SpeechRecognition" in window || "webkitSpeechRecognition" in window) {
// Speech Recognition is supported
}

That’s all folks! 🎉

Finding personal finance too intimidating? Checkout my Instagram to become a Dollar Ninja

Follow me for bi-weekly new tidbits on the domain of tech!

Need a Top Rated Front-End Development Freelancer to chop away your development woes? Contact me on Upwork

Want to see what I am working on? Check out my Personal Website and GitHub

Want to connect? Reach out to me on LinkedIn

I am a Digital Nomad and occasionally travel. Follow me on Instagram to check out what I am up to.

--

--

Tapajyoti Bose
Tapajyoti Bose

Written by Tapajyoti Bose

Top Rated Freelancer || Blogger || Cross-Platform App Developer || Web Developer || Open Source Contributor || FIRE Enthusiast