Sample questions
Question 1Easy
localStorage.setItem("n", 5) is run. What does localStorage.getItem("n") + 1 return?
- NaN
- "6"
- 6
- "51"
Answer: D. "51"
Explanation
Web Storage stores strings, so 5 becomes "5" and "5" + 1 concatenates to "51".
Step-by-step solution
- localStorage stores values as strings, so "5" is stored.
- getItem returns "5".
- "5" + 1 is string concatenation, giving "51".
Why the other options are wrong
- A. No NaN arises; + with a string concatenates.
- B. That would be numeric addition then string.
- C. Values are not numbers.
Tip: Storage returns strings.
Question 2Easy
How do you store an object in localStorage so it can be read back as an object?
- Call
toString() and parse it back with eval JSON.stringify it, then JSON.parse on read- Store each property under one shared key
- Pass the object directly to
setItem
Answer: B. JSON.stringify it, then JSON.parse on read
Explanation
setItem converts values to strings; an object becomes "[object Object]" unless you serialise it as JSON.
Step-by-step solution
- SetItem stores values as strings only.
- An object would become the text [object Object] unless it is serialised.
- JSON.stringify turns the object into JSON text before storing.
- So the correct answer is JSON.stringify on write and JSON.parse on read.
Why the other options are wrong
- A. ToString gives [object Object] and eval is unsafe.
- C. Splitting into shared keys is awkward and not how objects are stored.
- D. Passing an object directly stores [object Object].
Tip: LocalStorage stores strings: JSON.stringify to save, JSON.parse to read.
Question 3Easy
Which storage is automatically sent to the server with matching HTTP requests?
- sessionStorage
- localStorage
- Cookies
- IndexedDB
Answer: C. Cookies
Explanation
Cookies are attached to requests for their domain/path; Web Storage and IndexedDB stay in the browser.
Step-by-step solution
- Cookies are attached to matching requests automatically.
- Web storage and IndexedDB stay in the browser.
- So cookies are the answer.
Why the other options are wrong
- A. sessionStorage is client-only.
- B. localStorage is client-only.
- D. IndexedDB is client-only.
Tip: Only cookies go to the server automatically.
Question 4Easy
What does localStorage.getItem("missing") return for a key that was never set?
- null
- It throws an error
- undefined
- ""
Answer: A. null
Explanation
getItem returns null for a missing key.
Step-by-step solution
- GetItem reads the value for a key.
- If the key was never set, there is no value.
- GetItem returns null in that case, without throwing.
- So the correct answer is null.
Why the other options are wrong
- B. GetItem does not throw for a missing key.
- C. Undefined is not returned; it is null.
- D. An empty string would only be returned if the key was set to an empty string.
Tip: GetItem returns null for a missing key; check for null before using the value.
Question 5Medium
What does the HttpOnly cookie attribute do?
- Hides it from JavaScript
- Allows the cookie on HTTP but not HTTPS
- Deletes the cookie when the tab closes
- Sends the cookie only over plain HTTP
Answer: A. Hides it from JavaScript
Explanation
HttpOnly cookies cannot be read by JavaScript, limiting theft through XSS.
Step-by-step solution
- HttpOnly is a cookie attribute.
- A cookie with HttpOnly cannot be read by JavaScript.
- This limits theft through XSS.
- So the correct answer is that it hides the cookie from JavaScript.
Why the other options are wrong
- B. HttpOnly is not about HTTP versus HTTPS.
- C. HttpOnly does not control when the cookie is deleted.
- D. HttpOnly is not about the plain HTTP protocol.
Tip: Set HttpOnly on session cookies so scripts cannot read them.
Question 6Medium
Why is localStorage access considered a potential performance issue on the main thread?
- It sends data to the server each time
- It can only store one key per page
- It encrypts every value on write
- Its API is synchronous and can block
Answer: D. Its API is synchronous and can block
Explanation
Web Storage calls are synchronous, so heavy use can stall rendering; IndexedDB is asynchronous.
Step-by-step solution
- LocalStorage calls are synchronous.
- Each call blocks the main thread until it finishes.
- Heavy use can therefore stall rendering and input.
- So the correct answer is that its API is synchronous and can block.
Why the other options are wrong
- A. LocalStorage does not send data to the server.
- B. LocalStorage can store many keys per origin.
- C. LocalStorage does not encrypt values.
Tip: Avoid heavy localStorage use on the main thread; IndexedDB is asynchronous.
Question 7Medium
Which browser storage is best for large amounts of structured data, including files and blobs?
- sessionStorage
- localStorage
- IndexedDB
- Cookies
Answer: C. IndexedDB
Explanation
IndexedDB is an asynchronous, transactional, indexed object store with far larger limits than Web Storage.
Step-by-step solution
- The question asks for large structured data, including files and blobs.
- Web Storage is limited and synchronous.
- IndexedDB is asynchronous, transactional and holds large object stores.
- So the correct answer is IndexedDB.
Why the other options are wrong
- A. SessionStorage is small and only lasts for the tab session.
- B. LocalStorage is small and only stores strings.
- D. Cookies are tiny and are sent with requests.
Tip: Small string settings go in localStorage; large structured data goes in IndexedDB.
Question 8Medium
Why is storing an auth token in localStorage often discouraged?
- localStorage values expire after one minute
- localStorage is shared with every other website
- Any XSS script on the page can read it
- Tokens are too large to fit in localStorage
Answer: C. Any XSS script on the page can read it
Explanation
JavaScript can read all of localStorage, so an XSS flaw can steal the token; HttpOnly cookies are not readable by scripts.
Step-by-step solution
- Any JavaScript on the page can read localStorage.
- If an XSS bug lets a script run, it can read the token.
- HttpOnly cookies are not readable by scripts, which is why they are preferred.
- So the correct answer is that any XSS script can read it.
Why the other options are wrong
- A. LocalStorage values do not expire after one minute.
- B. LocalStorage is per origin and not shared with other websites.
- D. Tokens are small and fit easily in localStorage.
Tip: Anything in localStorage is readable by any script on the page.
Question 9Hard
A cookie must be sent on cross-site requests (for example inside a third-party iframe). Which attributes are required?
- SameSite=Strict; HttpOnly
- SameSite=None; Secure
- Domain=*; HttpOnly
- SameSite=Lax; Secure
Answer: B. SameSite=None; Secure
Explanation
SameSite=None opts into cross-site sending, and browsers require it to be paired with Secure.
Step-by-step solution
- Cross-site cookies need SameSite=None.
- Browsers require Secure with SameSite=None.
- So the pair is SameSite=None; Secure.
Why the other options are wrong
- A. Strict blocks all cross-site sends.
- C. Domain=* is not valid.
- D. Lax is not sent in third-party iframes.
Tip: Cross-site cookie: None + Secure.
Question 10Hard
What does SameSite=Lax on a cookie do?
- Withholds it on cross-site subrequests but sends it on top-level links
- Makes it readable only by the exact page path that set it
- Sends it on every cross-site request including images, iframes and POSTs
- Sends it only when the request comes from JavaScript fetch calls
Answer: A. Withholds it on cross-site subrequests but sends it on top-level links
Explanation
Lax withholds the cookie on cross-site subrequests and cross-site POSTs, but sends it on top-level GET navigations, mitigating CSRF.
Step-by-step solution
- SameSite=Lax limits cookies on cross-site requests.
- Cross-site subrequests such as images and iframes do not get the cookie.
- Top-level navigations such as clicking a link do get it.
- So the correct answer is withholding it on cross-site subrequests but sending it on top-level links.
Why the other options are wrong
- B. SameSite is about sites, not the exact page path.
- C. Lax does not send cookies on every cross-site request; that is SameSite=None.
- D. SameSite is not about JavaScript fetch calls.
Tip: Lax: sent on top-level GET links, not on cross-site subrequests.
Practise all 14 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →