JavaScript / JavaScript Local Storage and Session Storage

Working with Session Storage

Working with Session Storage tutorial will teach you how to manage data that only needs to be stored temporarily. You will learn how to use Session Storage to store, retrieve, and…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Teaches how to work with local storage and session storage for data persistence.

Working with Session Storage

1. Introduction

What is Session Storage?

Session Storage is a web storage object which stores data for one session. The data is deleted when the user closes the specific browser tab. It's a part of the Web Storage API, and allows you to store data on a user's web browser similar to cookies, but it's faster and much easier to work with.

Goals

In this tutorial, you will learn how to manage data that only needs to be stored temporarily. You'll learn how to use Session Storage to store, retrieve, and delete data.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of HTML, CSS, and JavaScript.

2. Step-by-Step Guide

Concept: Session Storage

Session Storage stores data as a key-value pair, and it can only store strings. To store objects, you will need to convert them to strings using JSON.stringify. Similarly, to retrieve objects, you will need to parse the string back into an object using JSON.parse.

Best Practices

  • Avoid storing sensitive user information in session storage as it's accessible through JavaScript and can be exploited.
  • Use session storage for non-sensitive data that needs to be maintained during page refreshes within a tab.

3. Code Examples

Example 1: Storing and Retrieving data from Session Storage

// Store data
sessionStorage.setItem('name', 'John');

// Retrieve data
let name = sessionStorage.getItem('name');

console.log(name); // Outputs: John

In the above code:
- setItem method is used to store data. It accepts two parameters - key and value.
- getItem method is used to retrieve data. It accepts one parameter - key.
- console.log is used to print the output to the console.

Example 2: Storing and Retrieving Objects from Session Storage

// Store object
let user = {name: 'John', age: 30};
sessionStorage.setItem('user', JSON.stringify(user));

// Retrieve object
let retrievedUser = JSON.parse(sessionStorage.getItem('user'));

console.log(retrievedUser); // Outputs: {name: 'John', age: 30}

In the above code:
- JSON.stringify is used to convert the object into a string before storing.
- JSON.parse is used to parse the string back into an object.

Example 3: Deleting data from Session Storage

// Remove specific data
sessionStorage.removeItem('name');

// Clear all data
sessionStorage.clear();

In the above code:
- removeItem method is used to remove data. It accepts one parameter - key.
- clear method is used to clear all data from session storage.

4. Summary

In this tutorial, we covered what session storage is, and how to store, retrieve, and delete data from it. As next steps, you might want to explore the other part of the Web Storage API - Local Storage, which is similar to Session Storage but persists even when the browser is closed.

5. Practice Exercises

Exercise 1

Store your favorite movie and its release year in session storage. Retrieve and print it to the console.

Exercise 2

Store an array of your favorite books in session storage. Retrieve and print them to the console.

Exercise 3

Store a list of your favorite songs with their artists in session storage as an object. Retrieve and print them to the console.

Solutions and explanations for these exercises can be found online, but it's recommended to try solving them on your own first. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Scientific Calculator

Perform advanced math operations.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

Image Converter

Convert between different image formats.

Use tool

Watermark Generator

Add watermarks to images easily.

Use tool

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help