Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

localStorage — local data storage

localStorage exposes a browser store of string key–value pairs associated with an origin. Data can outlast a session, but access and persistence depend on browser policy.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

window.localStorage
localStorage.setItem(key, value)
localStorage.getItem(key)
localStorage.removeItem(key)

window.localStorage returns a Storage object for the page's origin. It is a Web API defined by the HTML Living Standard, not an ECMAScript feature. Current browser engines support it; Node 24 in the repository does not establish browser support.

setItem(key, value) stores text, getItem(key) returns text or null, and removeItem(key) deletes an entry. clear() deletes every entry in that storage area, not just the application's entries.

Unlike sessionStorage, data can outlast a session and be shared between same-origin windows. It is not a backup: users or browsers can delete it. Do not assume locking for read–modify–write operations. Changes trigger a storage event in other windows using that storage area, not in the writing window.

Access itself can throw SecurityError, and writing can throw QuotaExceededError; handle both. Same-origin scripts can read the data, so do not store secrets here. When the operations succeed, the example writes a value under a randomly generated key, reads it back, and removes the entry without restoring any previous value. In the run shown, accessing window.localStorage throws SecurityError before the code reaches the write.

Examples

(() => {
  const key = `cktis-storage-${crypto.getRandomValues(new Uint32Array(4)).join('-')}`;
  try {
    const storage = window.localStorage;
    storage.setItem(key, 42);
    let stored;
    try {
      stored = storage.getItem(key);
    } finally {
      storage.removeItem(key);
    }
    console.log(`Stored as a string: ${stored === '42'}`);
    console.log(`Missing after removal: ${storage.getItem(key) === null}`);
  } catch (error) {
    console.log(`Storage unavailable: ${error.name}`);
  }
})();

Result

Storage unavailable: SecurityError

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources