Interviewer: Can sessionStorage Share Data Between Multiple Tabs?

The question that most front-end developers don’t know the answer

fatfish

--

Photo by Pietra Schwarzler on Unsplash

Recently, a friend of mine was stumped by a question about sessionStorage during an interview.

Well, I have to admit, I’m not familiar with it. My good friend, do you understand it?

Can sessionStorage share data between multiple tabs?

About the interview process

Question 1: “Do you know what is the difference between localStorage and sessionStorage?”

My friend: He felt so happy because this is the knowledge that any front-end development engineer knows.

  1. The data of localStorage is persistent, as long as we do not actively clear it, it will always exist.
  2. Closing a tab/window ends the session and clears objects in sessionStorage.

Question 2: “Can localStorage share data under the same website?”

My friend: “This is another easy one! Data can be shared between different tabs or windows under the same website”

// Let's try to set a name in window 1 page 1
localStorage.setItem('name', 'fatfish')
// We try to read the current name in window 2 page 2
localStorage.getItem('name') // fatfish

Question 3: “You are great, I want to test you one more question, Can sessionStorage share data between multiple tabs”

My friend: “No, each window or tab has a separate sessionStorage, there is no data sharing between them”

Interviewer: “Are you really sure that’s the case?”

My friend: “Eh! I’m not sure, maybe!”

Maybe my friend missed the offer because of this question but as a developer, we need to improve our knowledge and skills constantly.

What is sessionStorage?

(from mdn)The read-only sessionStorage property accesses a session Storage object for the current origin. sessionStorage is similar to localStorage; the difference is that while data in localStorage doesn’t expire, data in sessionStorage is cleared when the page session ends.

--

--

fatfish

Hi friends, I am a front-end engineer from Alibaba, let’s code happily together.