Web APIs are a set of built-in JavaScript objects that can provide an access to the browser's environment and allow web applications to interact with hardware, network resources, and other parts of the system through the browser.
- Fetch API
- WebSockets
- Local Storage
- Session Storage
- Geolocation API
- Canvas API
- Web Audio API
- Clipboard API
- Web camera API
- Microphone API
- Handling Cookies
- Other APIs
A modern API for making network requests for the data required in JavaScript.
Syntax:
fetch(url, options)
.then(response => response.json())
.then(data => {
// Handle the data
})
.catch(error => {
// Handle errors
});
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
console.log(data);
});
A protocol for establishing persistent, full-duplex communication channels between a client and a server.
Syntax:
const socket = new WebSocket('ws://example.com:8080');
const socket = new WebSocket('ws://example.com:8080');
socket.onopen = () => {
console.log('WebSocket connection opened');
};
socket.onmessage = (event) => {
console.log('Received message:', event.data);
};
socket.onclose = () => {
console.log('WebSocket connection closed');
};
It allows to store data locally in the browser for a specific domain.
/* Setting local storage value for specific key */
localStorage.setItem('key', 'value');
/* Reading local storage for the key */
let value = localStorage.getItem('key');
/* Removing local storage value on the key */
localStorage.removeItem('key');
/* Clearing local storage on the current site */
localStorage.clear();
It allows to store data locally in the browser for a single session.
/* Setting session storage */
sessionStorage.setItem('cart', JSON.stringify(cart));
/* Reading session storage */
let cart = JSON.parse(sessionStorage.getItem('cart'));
It provides access to the user's geographical location.
navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position.coords.latitude, position.coords.longitude);
},
(error) => {
console.error('Error:', error);
}
);
It allows to create 2d graphics on an HTML canvas element.
let canvas = document.getElementById('myCanvas');
let context = canvas.getContext('2d');
context.fillStyle = 'red';
context.fillRect(10, 10, 100, 100);
See More on this Canvas Reference
It provides an interface for creating, manipulating, and processing audio data.
Syntax:
let audioContext = new AudioContext();
audioContext.createOscillator().start();
It provides access to the user's clipboard.
navigator.clipboard.writeText('Hello, world!');
navigator.clipboard.readText().then(text => console.log(text));
It provides access to the user's webcam.
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
let video = document.getElementById('myVideo');
video.srcObject = stream;
})
.catch(error => {
console.error('Error:', error);
});
It provides access to the user's microphone.
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
let audioContext = new AudioContext();
let source = audioContext.createMediaStreamSource(stream);
// ... (process audio data)
})
.catch(error => {
console.error('Error:', error);
});
Cookies are small pieces of data stored on the user's computer by a web server as same as local storage.
/* Setting Cookies */
document.cookie = 'name=Vel Murugan; expires=Thu, 31 Dec 2024 12:00:00 GMT';
/* Reading Cookies */
let cookies = document.cookie.split(';');
- Battery Status API: Provides information about the device's battery status.
- Vibration API: Allows the device to vibrate.
- Device Orientation API: Provides information about the device's orientation.
- Device Motion API: Provides information about the device's acceleration and rotation.
- Pointer Lock API: Allows the user to lock the pointer to an element