-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal.html
More file actions
33 lines (30 loc) · 1.14 KB
/
local.html
File metadata and controls
33 lines (30 loc) · 1.14 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Local Storage Topic</title>
</head>
<body>
<h2>Local Storage</h2>
<script>
//store mysore information in a variable called as city
localStorage.setItem('city', 'mysore');
localStorage.setItem('population', 100000); // will still be stored as string '100000'
//persist page reload or when browser closed and opened
//read the key
console.log(localStorage.getItem('city'));
//I.Q). how to store an object in localstorage ?
const person = {
name: 'arun',
email: 'arun@gmail.com'
};
//by default person.toString() -> [object, object]
// while storing JSON.stringify() -> [used to convert object to string]
localStorage.setItem('person', JSON.stringify(person));
//while reading JSON.parse()
console.log('name: ', JSON.parse(localStorage.getItem('person')).name);
console.log('email: ', JSON.parse(localStorage.getItem('person')).email);
</script>
</body>
</html>