Working with the localStorage data in JavaScript

The setItem method is for saving data. It takes a key as its first parameter and a value as its second parameter. The getItem method is for getting data. It takes one parameter - the key under which this data was stored.

Let's try these methods in practice. First, let's store some string with some key:

localStorage.setItem('key', 'text');

And now retrieve our string from local storage:

let str = localStorage.getItem('key'); console.log(str); // shows 'text'

Write a script that will store three numbers under three different keys in local storage. Run this script to save the data. Then write a script that will retrieve your three numbers from local storage and find their sum. Run the second script and make sure that it works.