Skip to main content Accessibility Feedback

Storing data just for the browsing session with vanilla JavaScript

Yesterday, we looked at how to use localStorage to save user data locally in the browser. Today, you’ll learn how to save data just for the current browsing session.

All you need is the sessionStorage API, which works just like localStorage API.

// Store data
var someTempData = 'The data that I want to store temporarily.';
sessionStorage.setItem('myTempDataKey', someTempData);

// Get data
var tempData = sessionStorage.getItem('myTempDataKey');

// Remove data
sessionStorage.removeItem('myTempDatakey');

Like localStorage, sessionStorage works in all modern browsers and back to IE8. It’s also subject to the same storage limits.


🚀 Make 2018 the year you master JavaScript! My pocket guides and mini courses are short, focused, and made for beginners. You can do this!

Have any questions or comments about this post? Email me at chris@gomakethings.com or contact me on Twitter at @ChrisFerdinandi.

Get Daily Developer Tips