How to access Session variables and set them in javascript?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The first thing to clarify is that browser JavaScript cannot directly read or write server-side session variables. If the session lives on the server, JavaScript must talk to the server through an HTTP request. If you only need per-tab client-side storage, then sessionStorage is the browser feature you want instead.
Server session versus browser sessionStorage
These two concepts are often confused:
- Server session data lives on the server and is usually linked to a session cookie.
sessionStoragelives in the browser and is scoped to one tab or window.
Client-side JavaScript can access sessionStorage directly:
That is useful for UI preferences or temporary non-sensitive state. It is not the same thing as reading a server framework's session object.
To access server session variables, call an API
If your server stores session data, expose it through an endpoint. Here is a simple Express example using express-session:
On the browser side, JavaScript reads and updates that server session through fetch:
This is the normal web pattern. The browser never reaches into server memory directly; it asks the server for data and sends updates back.
Embedding session-derived data in the page
If you only need the session value during the initial page render, the server can inject it into the HTML:
Then client-side JavaScript can read it:
This works well for server-rendered apps, but it is still the server deciding what to expose. It is not direct access to the internal session store.
Security and practical boundaries
Because sessions often contain sensitive state, do not mirror the whole session into JavaScript just for convenience. Expose only the data the UI needs.
Also remember that JavaScript cannot mark a cookie as HttpOnly or read an HttpOnly cookie. That is by design. If your authentication session depends on an HttpOnly cookie, the browser will send it automatically with requests, but client-side code cannot inspect it directly.
That distinction is one reason secure session architectures use server-managed sessions plus HttpOnly cookies rather than storing everything in local browser storage.
Common Pitfalls
The biggest mistake is assuming sessionStorage is the same as server session state. It is not. One is client-side tab storage, and the other is server-side application state.
Another common problem is trying to read framework session variables directly from frontend JavaScript. That is impossible unless the server exposes them through HTML or an API response.
Developers also forget credentials when using fetch. If session cookies are required, configure the request so the browser sends them appropriately for your application setup.
Finally, do not expose sensitive session data to the frontend unless the UI truly needs it. Keeping session state server-side is often the whole point.
Summary
- Browser JavaScript cannot directly access server-side session variables.
- Use
sessionStorageonly for client-side per-tab state. - To read or update server session data, call an API endpoint and let the server modify the session.
- Server-rendered pages can embed selected session-derived values into HTML for initial use.
- Treat session data as sensitive and expose only what the frontend actually needs.
Related reading
- How to access the first property of a Javascript object?
- How to add 30 minutes to a JavaScript Date object?
- How to add new elements to an array?
- How to align a <div> to the middle (horizontally/width) of the page
- How to align content of a div to the bottom
- How to align text input correctly in react native?
- How to allow only numeric (0-9) in HTML inputbox using jQuery?
- How to always show scrollbar
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.