लोकल स्टोरेज में कुंजी जांचना
HTML5 localStorage API में कुंजी मौजूद है या नहीं, यह जांचने के कई तरीके हैं।
विधि 1: getItem() और null जांच (अनुशंसित)
// कुंजी मौजूद है और मान null नहीं है
if (localStorage.getItem('user') !== null) {
console.log('कुंजी मौजूद है:', localStorage.getItem('user'));
} else {
console.log('कुंजी मौजूद नहीं है या मान null है');
}
// छोटा रूप
const user = localStorage.getItem('user');
if (user) {
// मान truthy है (null, undefined, खाली स्ट्रिंग नहीं)
}
विधि 2: in ऑपरेटर
if ('user' in localStorage) {
console.log('कुंजी मौजूद है (मान null भी हो सकता है)');
} else {
console.log('कुंजी मौजूद नहीं है');
}
विधि 3: hasOwnProperty (Object.prototype पर)
if (localStorage.hasOwnProperty('user')) {
console.log('कुंजी मौजूद है');
}
विधि 4: try-catch के साथ (निजी/इनकॉग्निटो मोड के लिए)
function hasLocalStorageKey(key) {
try {
return key in localStorage;
} catch (e) {
// निजी मोड / स्टोरेज अक्षम
console.warn('लोकल स्टोरेज अनुपलब्ध:', e);
return false;
}
}
तुलना तालिका
getItem(key) !== null |
नहीं (false लौटाता है) | नहीं | सबसे आम; मान भी प्राप्त करता है |
key in localStorage |
हाँ (true लौटाता है) | नहीं (आमतौर पर) | केवल उपस्थिति जांच, मान नहीं चाहिए |
hasOwnProperty(key) |
हाँ | हाँ (निजी मोड में) | कम आम |
पूर्ण उदाहरण: उपयोगकर्ता सत्र प्रबंधन
<!DOCTYPE html>
<html>
<body>
<h2>लोकल स्टोरेज कुंजी जांच डेमो</h2>
<div style="margin-bottom: 15px;">
<input type="text" id="keyInput" placeholder="कुंजी नाम" value="user">
<input type="text" id="valueInput" placeholder="मान" value="जॉन">
<button onclick="setItem()">सेट करें</button>
<button onclick="checkKey()">जांचें</button>
<button onclick="removeItem()">हटाएँ</button>
<button onclick="clearAll()">सभी साफ़ करें</button>
</div>
<div style="margin-bottom: 15px;">
<strong>परिणाम:</strong>
<pre id="result" style="background:#f5f5f5; padding:10px; border-radius:4px;"></pre>
</div>
<h3>वर्तमान स्टोरेज सामग्री:</h3>
<div id="storageView"></div>
<script>
const result = document.getElementById('result');
const storageView = document.getElementById('storageView');
function updateStorageView() {
storageView.innerHTML = '';
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
const value = localStorage.getItem(key);
const div = document.createElement('div');
div.style.cssText = 'padding:5px; border-bottom:1px solid #eee; font-family:monospace;';
div.textContent = `${key}: "${value}"`;
storageView.appendChild(div);
}
if (localStorage.length === 0) {
storageView.textContent = '(खाली)';
}
}
function setItem() {
const key = document.getElementById('keyInput').value.trim();
const value = document.getElementById('valueInput').value;
if (key) {
localStorage.setItem(key, value);
log(`सेट: ${key} = "${value}"`);
updateStorageView();
}
}
function checkKey() {
const key = document.getElementById('keyInput').value.trim();
if (!key) return log('कुंजी दर्ज करें');
const exists = key in localStorage;
const value = localStorage.getItem(key);
const getItemNull = localStorage.getItem(key) !== null;
log(`कुंजी: "${key}"`);
log(` 'in' ऑपरेटर: ${exists}`);
log(` getItem() !== null: ${getItemNull}`);
log(` मान: "${value}"`);
}
function removeItem() {
const key = document.getElementById('keyInput').value.trim();
if (key) {
localStorage.removeItem(key);
log(`हटाया: ${key}`);
updateStorageView();
}
}
function clearAll() {
localStorage.clear();
log('सभी साफ़ किया गया');
updateStorageView();
}
function log(msg) {
result.textContent += msg + '\n';
result.scrollTop = result.scrollHeight;
}
// प्रारंभिक दृश्य
updateStorageView();
</script>
</body>
</html>
सत्र स्टोरेज (sessionStorage)
उसी API का उपयोग sessionStorage के लिए करें (टैब/विंडो बंद होने पर साफ़ हो जाता है):
if (sessionStorage.getItem('tempData') !== null) {
// सत्र-विशिष्ट डेटा
}
TypeScript में टाइप-सेफ जांच
function getLocalStorageItem(key: string, defaultValue: T): T {
try {
const item = localStorage.getItem(key);
return item !== null ? JSON.parse(item) : defaultValue;
} catch {
return defaultValue;
}
}
function setLocalStorageItem(key: string, value: T): void {
try {
localStorage.setItem(key, JSON.stringify(value));
} catch (e) {
console.error('लोकल स्टोरेज सेट त्रुटि:', e);
}
}