CKTIS / JavaScript
Storage: counting entries and reading key names
Storage exposes string key–value pairs through localStorage and sessionStorage. Learn the difference between Storage.length, which counts entries, and Storage.key(index), which returns a key name rather than its value.
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.length
storage.key(index)
storage.getItem(key)
storage.setItem(key, value)
storage.removeItem(key)
storage.clear()Storage is a Web API interface defined by HTML, not ECMAScript. Obtain its objects through window.localStorage or window.sessionStorage. The former is intended for storage beyond the current session; the latter is tied to a tab's session and origin. Keys and values are strings.
Storage.length is read-only: it gives the number of pairs, not their size in bytes. For Storage.key(index), Web IDL first converts the argument to an unsigned long. The method uses the converted value as a zero-based index and returns a key name, or null if that index is at least length. For example, storage.key(2 ** 32) uses index 0 and returns a key name if storage is nonempty. Read the value with getItem(key).
Key order is unspecified and may change upon mutations. Do not assume insertion order or remove entries while iterating by index. For reproducible presentation, collect the names and sort them.
Access can throw SecurityError, and writing can throw QuotaExceededError. Handle these errors in applications. The example first tries window.sessionStorage. If it catches SecurityError or QuotaExceededError, it uses an in-memory test fixture instead of Web Storage and prints the same console output. Having Node 24 in a repository does not establish support for this Web API.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Storage: counting entries and reading key names</title>
</head>
<body>
<h1>Counting storage entries and listing their keys</h1>
<p id="source"></p>
<script>
function inspectStorage(storage) {
const names = ['cktis:storage:a', 'cktis:storage:b'].map(base => {
let name = base;
let suffix = 0;
while (storage.getItem(name) !== null) name = `${base}:${++suffix}`;
return name;
});
const written = [];
try {
const before = storage.length;
for (const [index, value] of ['10', '20'].entries()) {
storage.setItem(names[index], value);
written.push(names[index]);
}
const count = storage.length;
const keys = [];
for (let index = 0; index < count; index++) {
const name = storage.key(index);
if (names.includes(name)) keys.push(name);
}
return [count - before, keys.length, storage.key(count) === null];
} finally {
for (const name of written) storage.removeItem(name);
}
}
let lines;
let source = 'Using browser sessionStorage.';
try {
lines = inspectStorage(window.sessionStorage);
} catch (error) {
if (error.name !== 'SecurityError' && error.name !== 'QuotaExceededError') throw error;
source = 'The browser storage test write is unavailable. Using an in-memory fixture for this demonstration.';
const entries = new Map();
lines = inspectStorage({
get length() { return entries.size; },
key(index) { return Array.from(entries.keys())[index] ?? null; },
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
2 2 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.