This chapter dives into working with JSON data and local storage in web development. You’ll learn about JSON parsing, storing and retrieving data, and clearing the local storage for efficient data management in the browser.
JSON Format and Parsing (JSON.parse, JSON.stringify)
Which method is used to convert a JavaScript object to a JSON string?
a) JSON.parse()
b) JSON.stringify()
c) JSON.convert()
d) JSON.toString()
How do you convert a JSON string back into a JavaScript object?
a) JSON.parse()
b) JSON.stringify()
c) JSON.object()
d) JSON.convert()
Which of the following is true about JSON format?
a) JSON keys must be in single quotes
b) JSON data can include functions
c) JSON keys must be in double quotes
d) JSON does not support arrays
What type of data can be stored in a JSON string?
a) Strings and numbers only
b) Objects, arrays, strings, numbers, booleans, null
c) Only objects
d) Only arrays
What does the JSON.stringify() method do in JavaScript?
a) Converts an object to JSON format
b) Parses a JSON string into an object
c) Converts an array into an object
d) Converts a string into an object
When using JSON.parse(), what happens if the input is not valid JSON?
a) It returns null
b) It throws an error
c) It ignores the invalid input
d) It returns an empty object
Which of the following types cannot be converted to JSON format?
a) Function
b) Array
c) Object
d) String
What is the output of JSON.stringify([1, 2, 3]) in JavaScript?
a) [1, 2, 3]
b) “1, 2, 3”
c) ‘[1, 2, 3]’
d) {“1”: 2, “2”: 3}
Which method is used to parse a JSON string into a JavaScript object?
a) JSON.parse()
b) JSON.stringify()
c) JSON.convert()
d) JSON.toObject()
What happens when you call JSON.stringify() on an object with functions inside it?
a) The functions are included in the output
b) The functions are excluded from the output
c) It throws an error
d) It converts functions into strings
Storing Data in Local Storage
Which web storage API is used to store data persistently across browser sessions?
a) Session Storage
b) Local Storage
c) Cookie Storage
d) Web Storage API
What type of data can be stored in Local Storage?
a) Only strings
b) Objects and arrays
c) Strings and numbers
d) Only primitive values
What is the maximum size limit for data that can be stored in Local Storage (in most browsers)?
a) 1MB
b) 5MB
c) 10MB
d) 50MB
Which method is used to store data in Local Storage?
a) localStorage.setItem()
b) localStorage.getItem()
c) localStorage.removeItem()
d) localStorage.clear()
How do you store a string value in Local Storage using JavaScript?
a) localStorage.storeItem(‘key’, ‘value’)
b) localStorage.addItem(‘key’, ‘value’)
c) localStorage.setItem(‘key’, ‘value’)
d) localStorage.set(‘key’, ‘value’)
Can you store objects directly in Local Storage?
a) Yes, directly as objects
b) Yes, but you must first serialize them into strings
c) No, only arrays can be stored
d) No, Local Storage only supports numbers
What happens if you try to store a new key-value pair in Local Storage with an existing key?
a) It overwrites the old value
b) It throws an error
c) It appends the new value to the old value
d) It ignores the new value
Which method would you use to check if a particular item exists in Local Storage?
a) localStorage.hasItem()
b) localStorage.getItem()
c) localStorage.containsItem()
d) localStorage.keyExists()
Which of the following can be used as a key when storing data in Local Storage?
a) Object
b) Array
c) String
d) Number
What happens if Local Storage exceeds its storage limit?
a) It throws an error
b) It ignores the new data
c) It starts deleting old data
d) It automatically compresses the data
Retrieving Data from Local Storage
How do you retrieve a value from Local Storage?
a) localStorage.getItem()
b) localStorage.get()
c) localStorage.retrieveItem()
d) localStorage.fetchItem()
What does the localStorage.getItem() method return if the key does not exist?
a) null
b) undefined
c) “undefined”
d) An empty string
How would you retrieve an item from Local Storage using a key “userData”?
a) localStorage.getItem(“userData”)
b) localStorage[“userData”]
c) localStorage.retrieve(“userData”)
d) localStorage.get(“userData”)
Which of the following is returned when you retrieve an item from Local Storage?
a) An object
b) A string
c) A JSON array
d) A function
After retrieving a JSON string from Local Storage, what must you do to convert it back to a JavaScript object?
a) Use JSON.parse()
b) Use JSON.stringify()
c) Use Object.create()
d) Nothing, it’s already an object
What will happen if you call JSON.parse() on an invalid JSON string retrieved from Local Storage?
a) It throws a SyntaxError
b) It returns null
c) It returns an empty object
d) It returns undefined
Which method would you use to check if a key exists in Local Storage before retrieving the value?
a) localStorage.keyExists()
b) localStorage.containsKey()
c) localStorage.getItem()
d) localStorage.hasOwnProperty()
How can you retrieve all keys stored in Local Storage?
a) localStorage.getAllKeys()
b) localStorage.key(index)
c) localStorage.getKeys()
d) localStorage.keys()
What is the purpose of the localStorage.length property?
a) It gives the number of items stored in Local Storage
b) It gives the size of the Local Storage data in bytes
c) It gives the first key stored in Local Storage
d) It returns a list of all keys in Local Storage
How can you retrieve all key-value pairs stored in Local Storage in a loop?
a) Use localStorage.key() and localStorage.getItem()
b) Use a forEach() loop
c) Use Object.keys(localStorage)
d) Use localStorage.items()
Answers
Qno
Answer
1
b) JSON.stringify()
2
a) JSON.parse()
3
c) JSON keys must be in double quotes
4
b) Objects, arrays, strings, numbers, booleans, null
5
a) Converts an object to JSON format
6
b) It throws an error
7
a) Function
8
c) ‘[1, 2, 3]’
9
a) JSON.parse()
10
b) The functions are excluded from the output
11
b) Local Storage
12
a) Only strings
13
b) 5MB
14
a) localStorage.setItem()
15
c) localStorage.setItem(‘key’, ‘value’)
16
b) Yes, but you must first serialize them into strings
17
a) It overwrites the old value
18
b) localStorage.getItem()
19
c) String
20
a) It throws an error
21
a) localStorage.getItem()
22
a) null
23
a) localStorage.getItem(“userData”)
24
b) A string
25
a) Use JSON.parse()
26
a) It throws a SyntaxError
27
d) localStorage.hasOwnProperty()
28
b) localStorage.key(index)
29
a) It gives the number of items stored in Local Storage
30
a) Use localStorage.key() and localStorage.getItem()