Syntax
localStorage.setItem(key, value)
localStorage.getItem(key)Examples
Basic Storage Operations
Storing, retrieving, and removing simple string values.
localStorage.setItem("username", "alice");
console.log(localStorage.getItem("username")); // "alice"
localStorage.removeItem("username");
console.log(localStorage.getItem("username")); // null
localStorage.setItem("theme", "dark");
localStorage.setItem("fontSize", "16");
localStorage.clear(); // removes everythingStoring Objects with JSON
localStorage only stores strings, so objects need to be serialized.
const userPrefs = { theme: "dark", fontSize: 16, notifications: true };
localStorage.setItem("preferences", JSON.stringify(userPrefs));
const stored = JSON.parse(localStorage.getItem("preferences"));
console.log(stored.theme); // "dark"
console.log(typeof stored); // "object" - properly restored, not just a stringlocalStorage vs sessionStorage
Choosing the right storage based on how long data should persist.
// Persists across browser restarts, until explicitly cleared
localStorage.setItem("rememberedEmail", "user@example.com");
// Cleared automatically when the tab/browser is closed
sessionStorage.setItem("currentStep", "3");
// Both share the identical API - only the lifetime differsBest practices
- Always wrap JSON.parse() calls on stored data in a try/catch, in case the stored value is missing or corrupted
- Use sessionStorage for temporary, per-tab data (like a multi-step form's progress), and localStorage for data that should persist long-term
- Never store sensitive information (passwords, tokens, personal data) in localStorage - it is accessible to any JavaScript running on the page, including malicious scripts
- Remember storage is per-origin (protocol + domain + port) and has a size limit (typically around 5-10MB) - it is not a substitute for a real database
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Arrow Functions
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Async/Await
Async/await is modern JavaScript syntax for handling asynchronous operations, making asynchronous code look and behave like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a Promise resolves. This approach makes asynchronous code more readable and easier to understand than traditional Promise chains or callbacks.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Data Types
JavaScript has seven primitive data types - string, number, boolean, undefined, null, bigint, and symbol - plus the object type, which includes arrays, functions, and plain objects. JavaScript is dynamically typed, meaning a variable can hold any type and that type can change. The typeof operator reports a value's type at runtime.
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Async/Await
Async/await is modern JavaScript syntax for handling asynchronous operations, making asynchronous code look and behave like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a Promise resolves. This approach makes asynchronous code more readable and easier to understand than traditional Promise chains or callbacks.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Data Types
JavaScript has seven primitive data types - string, number, boolean, undefined, null, bigint, and symbol - plus the object type, which includes arrays, functions, and plain objects. JavaScript is dynamically typed, meaning a variable can hold any type and that type can change. The typeof operator reports a value's type at runtime.