Back to Exercises

Storage (localStorage/session)

20 Exercises Available

Save user preferences, drafts, tokens — safe read/write.

1.Easy

Save JSON to localStorage

Create a function `save(key, value)` that JSON stringifies and saves to localStorage.

Sample Output:

save("theme", "dark")
// localStorage.setItem("theme", '"dark"') executed
save("user", {name: "John", age: 30})
// localStorage.setItem("user", '{"name":"John","age":30}') executed
View Solution
2.Medium

Load JSON from localStorage (safe)

Create a function `load(key, fallback=null)`. Return fallback for invalid JSON.

Sample Output:

load("theme", "light") => "dark" (if stored)
load("invalid", null) => null
load("user", {}) => {name: "John", age: 30} (if stored)
View Solution
3.Easy

Remember Last Visited Page

Create functions `rememberPath(path)` to save and `getRememberedPath()` to load, default "/".

Sample Output:

rememberPath("/products/123")
getRememberedPath() => "/products/123"
getRememberedPath() => "/" (if not set)
View Solution
4.Easy

Session Storage for OTP Timer

Create functions `setOtpExpiry(ts)` and `isOtpExpired()`. Expired if Date.now() > ts.

Sample Output:

setOtpExpiry(Date.now() + 60000)
isOtpExpired() => false (if < 1 minute)
isOtpExpired() => true (if > 1 minute)
View Solution
5.Medium

Clear Keys by Prefix

Create a function `clearByPrefix(prefix)` that deletes matching keys from localStorage.

Sample Output:

clearByPrefix("user_")
// Deletes: user_name, user_email, user_id (if exist)
clearByPrefix("cache_")
// Deletes: cache_posts, cache_users (if exist)
View Solution
6.Medium

Storage with Expiry Time

Function `setWithExpiry(key, value, ttl)` banao jo value ko expiry time ke saath store kare. Example: setWithExpiry("token", "abc123", 3600000) → expires after 1 hour

Sample Output:

setWithExpiry("token", "abc123", 3600000)
// Stores with 1 hour expiry
View Solution
7.Medium

Get Storage Value with Expiry Check

Function `getWithExpiry(key)` banao jo value get kare aur expired ho to null return kare. Example: getWithExpiry("token") => value or null if expired

Sample Output:

getWithExpiry("token") => "abc123" (if not expired)
getWithExpiry("token") => null (if expired)
View Solution
8.Medium

Storage Size Calculator

Function `getStorageSize()` banao jo localStorage ka total size calculate kare in bytes. Example: getStorageSize() => 1024 (bytes)

Sample Output:

getStorageSize() => 1024 (bytes)
// Returns total storage size
View Solution
9.Medium

Storage Namespace Helper

Function `createNamespace(prefix)` banao jo namespaced storage functions return kare. Example: const ns = createNamespace("app_"); ns.set("key", "value")

Sample Output:

const ns = createNamespace("app_");
ns.set("key", "value"); ns.get("key") => "value"
View Solution
10.Easy

Storage Event Listener

Function `onStorageChange(callback)` banao jo storage changes ko listen kare. Example: onStorageChange((key, newValue) => console.log(key, newValue))

Sample Output:

onStorageChange((key, newValue) => console.log(key, newValue))
// Listens to storage changes
View Solution
11.Easy

Storage with Version

Function `setVersioned(key, value, version)` banao jo value ko version ke saath store kare. Example: setVersioned("data", {x: 1}, "v1")

Sample Output:

setVersioned("data", {x: 1}, "v1")
// Stores with version
View Solution
12.Easy

Get Storage Value with Version Check

Function `getVersioned(key, expectedVersion)` banao jo value get kare aur version match kare. Example: getVersioned("data", "v1") => value or null if version mismatch

Sample Output:

getVersioned("data", "v1") => {x: 1} (if version matches)
getVersioned("data", "v2") => null (if mismatch)
View Solution
13.Hard

Storage with Encryption (simple)

Function `setEncrypted(key, value, password)` banao jo value ko encrypt karke store kare. Example: setEncrypted("secret", "data", "password123")

Sample Output:

setEncrypted("secret", "data", "password123")
// Stores encrypted value
View Solution
14.Hard

Get Decrypted Storage Value

Function `getDecrypted(key, password)` banao jo encrypted value ko decrypt kare. Example: getDecrypted("secret", "password123") => "data"

Sample Output:

getDecrypted("secret", "password123") => "data"
// Decrypts and returns value
View Solution
15.Medium

Storage with Compression

Function `setCompressed(key, value)` banao jo large value ko compress karke store kare. Note: Simple compression using JSON stringify.

Sample Output:

setCompressed("large", {x: 1, y: 2})
// Stores compressed value
View Solution
16.Medium

Storage Migration Helper

Function `migrateStorage(oldKey, newKey, transform)` banao jo old key se new key me data migrate kare. Example: migrateStorage("old", "new", (v) => v * 2)

Sample Output:

migrateStorage("old", "new", (v) => v * 2)
// Migrates and transforms data
View Solution
17.Hard

Storage Quota Check

Function `checkStorageQuota()` banao jo available storage space check kare. Example: checkStorageQuota() => {used: 1024, available: 5120000}

Sample Output:

await checkStorageQuota() => {used: 1024, available: 5120000}
// Returns storage usage info
View Solution
18.Medium

Storage with TTL and Auto Cleanup

Function `setWithTTL(key, value, ttl)` banao jo TTL ke saath store kare aur auto cleanup kare. Example: setWithTTL("temp", "data", 60000) → auto removes after 1 minute

Sample Output:

setWithTTL("temp", "data", 60000)
// Auto removes after 1 minute
View Solution
19.Easy

Storage Backup and Restore

Function `backupStorage()` aur `restoreStorage(backup)` banao jo storage ko backup/restore kare. Example: const backup = backupStorage(); restoreStorage(backup)

Sample Output:

const backup = backupStorage();
restoreStorage(backup)
// Backs up and restores storage
View Solution
20.Easy

Storage with Validation

Function `setValidated(key, value, validator)` banao jo value ko validate karke store kare. Example: setValidated("age", 25, (v) => v > 0 && v < 150)

Sample Output:

setValidated("age", 25, (v) => v > 0 && v < 150)
// Validates before storing
View Solution