Skip to content

Commit 391e1f9

Browse files
committed
localStorage snippets
1 parent a112313 commit 391e1f9

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Clear all items from localStorage
3+
description: Removes all values in localStorage, no matter what key they're under.
4+
author: JanluOfficial
5+
tags: localStorage,storage
6+
---
7+
8+
```js
9+
localStorage.clear();
10+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Get all keys from localStorage
3+
description: Gets all the keys currently assigned in localStorage.
4+
author: JanluOfficial
5+
tags: localStorage,storage
6+
---
7+
8+
```js
9+
function getAllKeysFromLocalStorage() {
10+
const keys = [];
11+
for (let i = 0; i < localStorage.length; i++) {
12+
keys.push(localStorage.key(i));
13+
}
14+
return keys;
15+
}
16+
17+
// Usage:
18+
getAllKeysFromLocalStorage(); // Returns all keys from localStorage
19+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Update Item in localStorage
3+
description: Updates an item with a specific key in localStorage.
4+
author: JanluOfficial
5+
tags: localStorage,storage
6+
---
7+
8+
```js
9+
function updateLocalStorageItem(key, newValue) {
10+
if (localStorage.getItem(key) !== null) {
11+
localStorage.setItem(key, newValue);
12+
} else {
13+
console.error(`Item with key "${key}" does not exist in LocalStorage.`);
14+
}
15+
}
16+
17+
// Usage:
18+
updateLocalStorageItem('username', 'newUsername');
19+
```

0 commit comments

Comments
 (0)