Storage Quota Management
Check and manage storage quota
JavaScript Storage Quota Management Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Check storage quota
async function getStorageQuota() {
if ('storage' in navigator && 'estimate' in navigator.storage) {
const estimate = await navigator.storage.estimate();
console.log('Quota:', estimate.quota);
console.log('Usage:', estimate.usage);
console.log('Available:', estimate.quota - estimate.usage);
console.log('Usage %:', ((estimate.usage / estimate.quota) * 100).toFixed(2) + '%');
return estimate;
}
return null;
}
getStorageQuota();
// Method 2: Check localStorage size
function getLocalStorageSize() {
let total = 0;
for (let key in localStorage) {
if (localStorage.hasOwnProperty(key)) {
total += localStorage[key].length + key.length;
}
}
return total;
}
const size = getLocalStorageSize();
console.log('LocalStorage size:', size, 'bytes');
console.log('Size in KB:', (size / 1024).toFixed(2));
// Method 3: Check if storage is full
function isStorageFull() {
try {
const test = '__storage_test__';
localStorage.setItem(test, 'test');
localStorage.removeItem(test);
return false;
} catch (e) {
return e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED';
}
}
if (isStorageFull()) {
console.log('Storage is full');
} else {
console.log('Storage has space');
}
// Method 4: Clear old data when full
function setItemWithCleanup(key, value) {
try {
localStorage.setItem(key, value);
} catch (e) {
if (e.name === 'QuotaExceededError') {
console.log('Storage full, clearing old data...');
// Clear items older than 7 days
clearOldItems(7);
// Try again
localStorage.setItem(key, value);
} else {
throw e;
}
}
}
function clearOldItems(days) {
const cutoff = Date.now() - (days * 24 * 60 * 60 * 1000);
for (let i = localStorage.length - 1; i >= 0; i--) {
const key = localStorage.key(i);
const item = localStorage.getItem(key);
try {
const data = JSON.parse(item);
if (data.timestamp && data.timestamp < cutoff) {
localStorage.removeItem(key);
}
} catch (e) {
// Not JSON, skip
}
}
}
// Method 5: Monitor storage usage
function monitorStorage() {
setInterval(async () => {
const estimate = await navigator.storage.estimate();
const usagePercent = (estimate.usage / estimate.quota) * 100;
if (usagePercent > 80) {
console.warn('Storage usage high:', usagePercent.toFixed(2) + '%');
}
}, 60000); // Check every minute
}
// Method 6: Request persistent storage
async function requestPersistentStorage() {
if ('storage' in navigator && 'persist' in navigator.storage) {
const isPersistent = await navigator.storage.persist();
console.log('Persistent storage granted:', isPersistent);
return isPersistent;
}
return false;
}
requestPersistentStorage();Quota: 10737418240 Usage: 1024000 Available: 10736394240 Usage %: 0.01% LocalStorage size: 5120 bytes Size in KB: 5.00 Storage has space Persistent storage granted: true
Understanding Storage Quota Management
Quota management prevents storage errors.
Storage Quota
QuotaExceededError
Monitoring
Persistent Storage
Best Practices
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.
Practical Learning Notes for Storage Quota Management
This JavaScript program is part of the "LocalStorage/SessionStorage" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.