Browser APIs

My reference for storage, timers, navigation, viewport information, and other browser-provided features.

JavaScript Beginner–Intermediate

Browser Storage

Storing and retrieving string-based data in the browser.

Storing and retrieving string-based data in the browser.
Term / Syntax Name Description Example
localStorage.setItem() Store Persistent Value Stores a string value under a specified key and keeps it across browser sessions. localStorage.setItem("theme", "dark");
localStorage.getItem() Read Persistent Value Returns the value stored under a specified key, or null if the key does not exist. const theme = localStorage.getItem("theme");
localStorage.removeItem() Remove Persistent Value Removes the value stored under a specified key. localStorage.removeItem("theme");
localStorage.clear() Clear Persistent Storage Removes all values stored in local storage for the current origin. localStorage.clear();
sessionStorage.setItem() Store Session Value Stores a string value for the current page session. sessionStorage.setItem("step", "2");
sessionStorage.getItem() Read Session Value Returns a session value stored under a specified key, or null if the key does not exist. const step = sessionStorage.getItem("step");
sessionStorage.removeItem() Remove Session Value Removes the value stored under a specified key for the current page session. sessionStorage.removeItem("step");
JSON.stringify() + localStorage Store Structured Data Converts a JavaScript value to JSON text before storing it in local storage. localStorage.setItem("user", JSON.stringify(user));
JSON.parse() + localStorage Read Structured Data Parses stored JSON text back into a JavaScript value. const user = JSON.parse(localStorage.getItem("user"));

Timers

Scheduling code to run after a delay or repeatedly at a specified interval.

Scheduling code to run after a delay or repeatedly at a specified interval.
Term / Syntax Name Description Example
setTimeout() Delayed Execution Schedules a function to run once after at least the specified delay in milliseconds. setTimeout(showMessage, 1000);
clearTimeout() Cancel Timeout Cancels a timeout before its scheduled function runs. clearTimeout(timeoutId);
setInterval() Repeated Execution Schedules a function to run repeatedly with approximately the specified delay between executions. const intervalId = setInterval(updateClock, 1000);
clearInterval() Cancel Interval Stops a repeating interval created with setInterval(). clearInterval(intervalId);

Location & Navigation

Reading the current URL and controlling browser navigation from JavaScript.

Reading the current URL and controlling browser navigation from JavaScript.
Term / Syntax Name Description Example
window.location.href Current URL Returns the current page URL or navigates to a new URL when assigned a value. window.location.href = "/about.html";
window.location.reload() Reload Page Reloads the current page. window.location.reload();
window.location.assign() Navigate to URL Navigates to a specified URL and keeps the current page in the browser's session history. window.location.assign("/about.html");
window.location.replace() Replace Current Page Navigates to a specified URL while replacing the current page in the browser's session history. window.location.replace("/login.html");

Browser Information

Reading common information provided by the browser and current viewport.

Reading common information provided by the browser and current viewport.
Term / Syntax Name Description Example
window.innerWidth Viewport Width Returns the width of the browser's layout viewport in CSS pixels. const width = window.innerWidth;
window.innerHeight Viewport Height Returns the height of the browser's layout viewport in CSS pixels. const height = window.innerHeight;
navigator.language Preferred Language Returns the user's preferred browser language as a language tag. const language = navigator.language;
navigator.onLine Online Status Indicates whether the browser reports that a network connection is available. if (navigator.onLine) { /* connected */ }