Skip to main content
localStorage - Storage - Web documentation
variable localStorage

Deno's localStorage API provides a way to store key-value pairs in a web-like environment, similar to the Web Storage API found in browsers. It allows developers to persist data across sessions in a Deno application. This API is particularly useful for applications that require a simple and effective way to store data locally.

  • Key-Value Storage: Stores data as key-value pairs.
  • Persistent: Data is retained even after the application is closed.
  • Synchronous API: Operations are performed synchronously.

localStorage is similar to sessionStorage, and shares the same API methods, visible in the Storage type.

When using the --location flag, the origin for the location is used to uniquely store the data. That means a location of http://example.com/a.ts and http://example.com/b.ts and http://example.com:80/ would all share the same storage, but https://example.com/ would be different.

For more information, see the reference guide for Web Storage and using the --location flag.

Examples

Example 1

// Set a value in localStorage
localStorage.setItem("key", "value");

// Get a value from localStorage
const value = localStorage.getItem("key");
console.log(value); // Output: "value"

// Remove a value from localStorage
localStorage.removeItem("key");

// Clear all values from localStorage
localStorage.clear();

Type

See

Back to top