Browser Storage Questions with Answers

14 practice questions on Browser Storage (Modern Web (Next.js, PWA, Performance)) with answers and step-by-step solutions. Try 10 samples below, then practise all 14 free in the AptiNest app.

Sample questions

Question 1Easy

localStorage.setItem("n", 5) is run. What does localStorage.getItem("n") + 1 return?

  1. NaN
  2. "6"
  3. 6
  4. "51"

Answer: D. "51"

Explanation

Web Storage stores strings, so 5 becomes "5" and "5" + 1 concatenates to "51".

Step-by-step solution

  1. localStorage stores values as strings, so "5" is stored.
  2. getItem returns "5".
  3. "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?

  1. Call toString() and parse it back with eval
  2. JSON.stringify it, then JSON.parse on read
  3. Store each property under one shared key
  4. 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

  1. SetItem stores values as strings only.
  2. An object would become the text [object Object] unless it is serialised.
  3. JSON.stringify turns the object into JSON text before storing.
  4. 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?

  1. sessionStorage
  2. localStorage
  3. Cookies
  4. 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

  1. Cookies are attached to matching requests automatically.
  2. Web storage and IndexedDB stay in the browser.
  3. 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?

  1. null
  2. It throws an error
  3. undefined
  4. ""

Answer: A. null

Explanation

getItem returns null for a missing key.

Step-by-step solution

  1. GetItem reads the value for a key.
  2. If the key was never set, there is no value.
  3. GetItem returns null in that case, without throwing.
  4. 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?

  1. Hides it from JavaScript
  2. Allows the cookie on HTTP but not HTTPS
  3. Deletes the cookie when the tab closes
  4. 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

  1. HttpOnly is a cookie attribute.
  2. A cookie with HttpOnly cannot be read by JavaScript.
  3. This limits theft through XSS.
  4. 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?

  1. It sends data to the server each time
  2. It can only store one key per page
  3. It encrypts every value on write
  4. 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

  1. LocalStorage calls are synchronous.
  2. Each call blocks the main thread until it finishes.
  3. Heavy use can therefore stall rendering and input.
  4. 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?

  1. sessionStorage
  2. localStorage
  3. IndexedDB
  4. Cookies

Answer: C. IndexedDB

Explanation

IndexedDB is an asynchronous, transactional, indexed object store with far larger limits than Web Storage.

Step-by-step solution

  1. The question asks for large structured data, including files and blobs.
  2. Web Storage is limited and synchronous.
  3. IndexedDB is asynchronous, transactional and holds large object stores.
  4. 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?

  1. localStorage values expire after one minute
  2. localStorage is shared with every other website
  3. Any XSS script on the page can read it
  4. 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

  1. Any JavaScript on the page can read localStorage.
  2. If an XSS bug lets a script run, it can read the token.
  3. HttpOnly cookies are not readable by scripts, which is why they are preferred.
  4. 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?

  1. SameSite=Strict; HttpOnly
  2. SameSite=None; Secure
  3. Domain=*; HttpOnly
  4. 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

  1. Cross-site cookies need SameSite=None.
  2. Browsers require Secure with SameSite=None.
  3. 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?

  1. Withholds it on cross-site subrequests but sends it on top-level links
  2. Makes it readable only by the exact page path that set it
  3. Sends it on every cross-site request including images, iframes and POSTs
  4. 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

  1. SameSite=Lax limits cookies on cross-site requests.
  2. Cross-site subrequests such as images and iframes do not get the cookie.
  3. Top-level navigations such as clicking a link do get it.
  4. 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 →

Related topics

See all Modern Web (Next.js, PWA, Performance) topics · All topics