Local storage is an important aspect of JavaScript and can be used in different scenarios. First, What is local storage: Local storage is part of the web storage API and it provides a way to store data in the browser.
Local storage allows you to store little sizes of data as key-value pairs, and it only allows storage of String data type, other data types such as numbers, objects, arrays, and booleans need to be serialized and deserialized.
There are various local storage methods used in storing, getting, deleting, and clearing data in local storage, some of which are mentioned below :
- getItem
- setItem
- removeItem.
- setItem method: This is used to store key-value pairs in the local storage, taking in two parameters {the key and value}. The key serves as an identifier of the value, and the value can be in the form of any valid Javascript data type. e.g. [localStorage.setItem(‘mykey’, ‘myValue’)].
- getItem method: This is used to retrieve the value associated with any specific key from the local storage. It takes the key as a parameter and returns its corresponding value. e.g [localStorage.getItem(‘myKey’)].
- removeItem method: This is used to remove or delete key-value pair from the local storage based on the specified key. It takes the key as a parameter and removes the corresponding key-value pair from the local storage if it exists. e.g [localStorage.removeItem(‘myKey’)].
There are many use cases for local storage and it is quite useful and will come in handy. FOLLOW FOR MORE CONTENT.
On Twitter: theonlyabdull
Top comments (0)