CKTIS / JavaScript
Storage.getItem — reading a Web Storage value
Storage.getItem(key) synchronously reads text from localStorage or sessionStorage. It returns null when the key does not exist; an empty string is an existing value, not a missing entry.
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.getItem(key)Storage.getItem is a Storage instance method used by localStorage and sessionStorage. It accepts a string key and returns the stored string, or null if the entry does not exist. Reading is synchronous and does not remove the entry.
Distinguish null, "", and "null". Check absence with value === null, not !value, which also matches an empty string. Stored numbers and objects do not automatically regain their types.
This is a Web API defined by the HTML Living Standard, not a feature of a particular ECMAScript edition. It is widely supported in modern browsers; the repository's Node version does not establish browser support. Accessing window.localStorage or window.sessionStorage itself can throw SecurityError because of an opaque origin or browser policy.
The example distinguishes an empty string, the string "42", and a missing entry. It chooses an unused key and removes the temporary entry to check that reading it returns null. The finally block repeats the removal to clean up even if an error occurs, without clearing the whole storage area.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Storage.getItem: empty and missing values</title>
</head>
<body>
<h1>Reading a storage value</h1>
<p id="source"></p>
<script>
function readValues(storage) {
let key = 'cktis:getItem';
while (storage.getItem(key) !== null) key += ':';
try {
storage.setItem(key, '');
const empty = storage.getItem(key);
storage.setItem(key, '42');
const text = storage.getItem(key);
storage.removeItem(key);
return [JSON.stringify(empty), JSON.stringify(text), storage.getItem(key) === null];
} finally {
storage.removeItem(key);
}
}
let lines;
let source = 'Using browser sessionStorage.';
try {
lines = readValues(window.sessionStorage);
} catch (error) {
if (error.name !== 'SecurityError' && error.name !== 'QuotaExceededError') throw error;
source = error.name === 'QuotaExceededError'
? 'Could not write the demo value to sessionStorage. Existing entries may still be readable. Using an in-memory fixture, not Web Storage.'
: 'Browser storage is unavailable. Using an in-memory fixture, not Web Storage.';
const entries = new Map();
lines = readValues({
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
"" "42" true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.