Creating a session storage entry

To create a key-value pair inside the sessionStorage object, you can use the setItem method, similar to the localStorage object. Just like localStorage, sessionStorage is also a synchronous API, so you can be sure that you'll immediately have access to whatever values you're storing.

Adding an item to session storage is just like working with local storage, as shown in the following snippet:

sessionStorage.setItem('my key', 'awesome value');console.log('Added to session storage');

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.