Local storage allows developers to store and retrieve data in the browser. The data stored in local storage will not expire. This means the data will persist even if the tab or the browser window is closed. Show PrerequisitesYou must have a basic understanding of JavaScript. You also need a code editor and browser to test the project. In this tutorial, we will be using Visual Studio Code and Google Chrome. What is local storageLocal storage is a form of web storage that stores data for a long time. This could be a day, a week, or even a year. This depends upon the developer’s preference. It is important to note that local storage only stores strings so, if you wish to store objects, lists, or arrays, you must convert them into a string using 6When to use local storageYou should only use local storage when storing insensitive information. This is because third-party individuals can easily access the information. Local storage can help in storing temporary data before it is pushed to the server. It is important to clear the local storage once this operation is completed. LimitationsThe major limitations of local storage are:
Main methods in local storageThe primary methods when using local storage are 7, 8, 9, 0, and 1.key()This method is used to retrieve a value/string from a specific location. The index can be passed into the 7 function as a parameter.
The 7 can also be used in a loop statement to retrieve all the items in the local storage.setItem()This function is used to store items in local storage. An example of this function is shown below.
As mentioned before, we must 4 objects before we store them in the local storage.An example is outlined below:
Failure to stringify the object will result in an error. getItem()This function is used to access or retrieve the data in the local storage. The method takes in a 5 as a parameter. It then extracts the required value from the localSstorage.For example, to retrieve the above 6 object, we will use the following statement:
The above statement will return something like this:
You should convert it to an object using 7 to use it in your code.
removeItem()This method is used to delete an item from local storage. The 9 method requires a key as a parameter.
clear()This method is used to clear all values stored in local storage. It does not require any parameters.
ProjectNow that we have learned about the primary functions of local storage, let’s create a web application that stores, retrieves, deletes, and clears items from local storage. Create a new folder and open it in your code editor. Create two files, 9 and 0. The 9 file will showcase the webpage to the user, while the 0 file will store our JavaScript functions. These functions will be used to access different functionalities of local storage.Let’s CodeOur 9 will have a 4 and several 5, as shown below.
When the 6 button is clicked, it takes the user input and passes it to the 7 function in the 0 file. The 9 gets the user input. These values are then passed to the car object and stored in local storage using the 0 method.
Similarly, the 1 will invoke the 2 function when clicked. This method fetches items from the localStorage using the getItem function. 3 creates a new paragraph component in our web page. 4 helps create the text that will be displayed to the user.The text node is then added to the paragraph tag by 5.These components are then shown in a specific place on the web page by 6 and 7. 0 8 invokes 9. This 0 will delete a value from the local storage using the 1 function. 1 2 calls the 3. The 1 method is used to remove all values in the local storage. 2Let’s set the 5 property of all the buttons when the webpage loads. 3Here is the 0 file with all the functions: 4As shown above, the functions will only be accessible after the page has finished loading. This is specified by the 7 method.Ensure that the 0 file is referenced in the 9 file by pasting the statement below in the 0 section. 5ResultsThe following video shows how the site works: ConclusionYou are now familiar with the different functionalities of local storage. The major methods in local storage are 0, 2, 1 and 4. A 5 is required when storing, retrieving, and removing items from the local storage. In case, you didn’t understand any concept, feel free to go through the local storage functions again.
Apa yang dimaksud dengan Web Storage?Web storage adalah salah satu Web API yang dapat menyimpan data secara lokal pada sisi client. Berbeda dengan objek atau array, data yang disimpan pada objek atau array JavaScript bersifat sementara, dan akan hilang jika terjadi reload atau pergantian URL pada browser.
Apa perbedaan antara localStorage dan Sessionstorage?Perbedaan antara local storage dan session storage yaitu, pada local storage data yang tersimpan tidak memiliki waktu expired (tidak ada batasan waktu penyimpanan), namun session storage data yang tersimpan akan hilang ketika kita menutup browser yang akan kita gunakan.
Apa kelebihan menyimpan data pada local storage dibandingkan dengan cookies?Local storage dapat menyimpan data lebih aman dan lebih besar dibandingkan dengan cookie. Local storage juga dapat menyimpan data lebih dari 5 MB tanpa harus membebani performa browser.
|