Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Storage.setItem — storing and replacing a value

Storage.setItem stores text under a key in localStorage or sessionStorage, replacing the previous value. Writes can fail, so account for exceptions and storage access restrictions.

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

storage.setItem(key, value)

Call Storage.setItem on a localStorage or sessionStorage object: storage.setItem(key, value). It creates an entry or replaces an existing key's value and returns undefined. This is an HTML-standard API, not a feature of a particular ECMAScript edition. Modern browser engines support it; having Node 24 in a repository does not establish browser API availability.

Keys and values are converted to strings. To store an object, explicitly serialize it to a string. When you read the stored value, storage returns a string, and your application must decode it according to the format it wrote. If you write JSON using JSON.stringify, use JSON.parse to decode that string. localStorage stores data for an origin across sessions, whereas sessionStorage is additionally separated by top-level browsing context, usually a tab. Data can be deleted.

A failed write can throw QuotaExceededError; accessing storage itself can throw SecurityError. Wrap both access and writing in try/catch if the application must work without storage. Writing identical text changes nothing. The storage notification targets other eligible documents, not the window performing the write.

Examples

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Storage.setItem: writing and replacing text</title>
</head>
<body>
<h1>Replacing a storage value</h1>
<p id="source"></p>
<script>
  function writeValues(storage) {
    let key = 'cktis:setItem';
    while (storage.getItem(key) !== null) key += ':';

    try {
      storage.setItem(key, 'first');
      const before = storage.getItem(key);
      storage.setItem(key, 'second');
      return [before, storage.getItem(key)];
    } finally {
      storage.removeItem(key);
    }
  }

  let lines;
  let source = 'Using browser sessionStorage.';
  try {
    lines = writeValues(window.sessionStorage);
  } catch (error) {
    if (error.name !== 'SecurityError' && error.name !== 'QuotaExceededError') throw error;
    source = 'Browser storage is unavailable. Using an in-memory fixture, not Web Storage.';
    const entries = new Map();
    lines = writeValues({
      getItem(key) { return entries.has(key) ? entries.get(key) : null; },
      setItem(key, value) { entries.set(key, String(value)); },
      removeItem(key) { entries.delete(key); }
    });
  }
  document.getElementById('source').textContent = source;
  for (const line of lines) console.log(line);
</script>
</body>
</html>

Result

first
second

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