Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
JAVASCRIPT STORAGE / CHAPTER 06 / Learn / LESSON 06
Theory. The browser has Web Storage: a small key → string table that survives a reload. It has four main methods: getItem(key), setItem(key, value), removeItem(key), clear(). Values are ONLY strings (that is why JSON for objects).
IMPORTANT in Code Lab: the sandbox has no access to the real localStorage. All lessons use window.labStorage — the same API, but in isolated memory for the lesson. On a real page you would write localStorage with the same calls. Do NOT write localStorage in these tasks — you will get an error.
Small examples:
window.labStorage.setItem("lang", "bg");
window.labStorage.getItem("lang"); // "bg"
window.labStorage.getItem("nope"); // null (missing key)
window.labStorage.getItem("empty"); // "" (key with an empty string)
window.labStorage.removeItem("lang");null and "" are DIFFERENT: null means "no record", "" means "a record exists but is empty".
Do not store passwords, tokens, or personal secrets in browser storage — any script on the page can read them.
Common mistake: if (!getItem(k)) — mixes missing and empty; saving a number without String; localStorage instead of labStorage here.
<p id="out"></p>{
"theme": "dark",
"old": "1",
"empty": ""
}getItem returns null for a missing key and "" for a key with an empty string — different states.
(empty)Press Run — HTML loads in a sandbox iframe; Stop removes the iframe (also kills infinite loops).
›