-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16-LocalStorage-SessionStorage-&-JSON.html
More file actions
103 lines (78 loc) · 2.95 KB
/
16-LocalStorage-SessionStorage-&-JSON.html
File metadata and controls
103 lines (78 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>16-LocalStorage-SessionStorage-&-JSON</title>
</head>
<body>
<script>
/* ================================
LOCAL STORAGE – BASIC EXAMPLE
================================ */
// Save data in localStorage (key-value pair)
localStorage.setItem("name", "JD");
// Get data from localStorage
let myName = localStorage.getItem("name");
console.log(myName); // Output: JD
// Remove single item from localStorage
// localStorage.removeItem("name");
// Clear all localStorage data
// localStorage.clear();
/* ================================
SESSION STORAGE – BASIC EXAMPLE
================================ */
// Save data in sessionStorage
// This data will be removed when the browser tab is closed
sessionStorage.setItem("city", "Delhi");
/* ================================
STORING OBJECT USING JSON
================================ */
// JavaScript object
let user = {
name: "JD",
age: 25,
};
// Convert object to JSON string before saving
localStorage.setItem("user", JSON.stringify(user));
/* ================================
STORING ARRAY USING JSON
================================ */
// JavaScript array
let fruits = ["apple", "pineapple"];
// Convert array to JSON string and save
localStorage.setItem("fruitsData", JSON.stringify(fruits));
// Get JSON string from localStorage
let fruitsData = localStorage.getItem("fruitsData");
console.log(fruitsData); // String format
/* ================================
JSON.parse() – CONVERT BACK
================================ */
// Get user data as string
let userData = localStorage.getItem("user");
console.log(userData); // JSON string
// Convert JSON string back to object
console.log(JSON.parse(userData));
/* ================================
THEME SAVE EXAMPLE
================================ */
// Save theme preference
localStorage.setItem("theme", "dark");
// Get theme from localStorage
let theme = localStorage.getItem("theme");
// Apply theme to body
document.body.className = theme;
/* ================================
TODO LIST STORAGE EXAMPLE
================================ */
// Todo list array
let todoList = ["Learn JS", "Practice Code"];
// Save todo list using JSON
localStorage.setItem("todos", JSON.stringify(todoList));
// Get todos from localStorage
// If no data exists, use empty array
let savedTodos = JSON.parse(localStorage.getItem("todos")) || [];
console.log(savedTodos);
</script>
</body>
</html>